UNPKG

@lexical/file

Version:

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

10 lines (8 loc) 1.42 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. * */ "use strict";var e=require("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 o(e,t){const o="string"==typeof t?JSON.parse(t):t;return e.parseEditorState(o.editorState)}exports.editorStateFromSerializedDocument=o,exports.exportFile=function(e,o=Object.freeze({})){const n=new Date;!function(e,t){const o=document.createElement("a"),n=document.body;if(null===n)return;n.appendChild(o),o.style.display="none";const r=JSON.stringify(e),i=new Blob([r],{type:"octet/stream"}),a=window.URL.createObjectURL(i);o.href=a,o.download=t,o.click(),window.URL.revokeObjectURL(a),o.remove()}(t(e.getEditorState(),{...o,lastSaved:n.getTime()}),`${o.fileName||n.toISOString()}.lexical`)},exports.importFile=function(t){!function(e){const t=document.createElement("input");t.type="file",t.accept=".lexical",t.addEventListener("change",t=>{const o=t.target;if(o.files){const t=o.files[0],n=new FileReader;n.readAsText(t,"UTF-8"),n.onload=t=>{if(t.target){const o=t.target.result;e(o)}}}}),t.click()}(n=>{t.setEditorState(o(t,n)),t.dispatchCommand(e.CLEAR_HISTORY_COMMAND,void 0)})},exports.serializedDocumentFromEditorState=t;