UNPKG

@lexical/file

Version:

This package provides the file import/export feature for Lexical.

10 lines (8 loc) 1.4 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{CLEAR_HISTORY_COMMAND as e}from"lexical";function t(e,t=Object.freeze({})){return{editorState:e.toJSON(),lastSaved:t.lastSaved||Date.now(),source:t.source||"Lexical",version:"0.37.0"}}function n(e,t){const n="string"==typeof t?JSON.parse(t):t;return e.parseEditorState(n.editorState)}function o(t){!function(e){const t=document.createElement("input");t.type="file",t.accept=".lexical",t.addEventListener("change",t=>{const n=t.target;if(n.files){const t=n.files[0],o=new FileReader;o.readAsText(t,"UTF-8"),o.onload=t=>{if(t.target){const n=t.target.result;e(n)}}}}),t.click()}(o=>{t.setEditorState(n(t,o)),t.dispatchCommand(e,void 0)})}function i(e,n=Object.freeze({})){const o=new Date;!function(e,t){const n=document.createElement("a"),o=document.body;if(null===o)return;o.appendChild(n),n.style.display="none";const i=JSON.stringify(e),a=new Blob([i],{type:"octet/stream"}),c=window.URL.createObjectURL(a);n.href=c,n.download=t,n.click(),window.URL.revokeObjectURL(c),n.remove()}(t(e.getEditorState(),{...n,lastSaved:o.getTime()}),`${n.fileName||o.toISOString()}.lexical`)}export{n as editorStateFromSerializedDocument,i as exportFile,o as importFile,t as serializedDocumentFromEditorState};