@lexical/file
Advanced tools
Comparing version 0.15.0 to 0.16.0
@@ -8,4 +8,32 @@ /** | ||
*/ | ||
import type { LexicalEditor } from 'lexical'; | ||
import type { EditorState, LexicalEditor, SerializedEditorState } from 'lexical'; | ||
export interface SerializedDocument { | ||
/** The serialized editorState produced by editorState.toJSON() */ | ||
editorState: SerializedEditorState; | ||
/** The time this document was created in epoch milliseconds (Date.now()) */ | ||
lastSaved: number; | ||
/** The source of the document, defaults to Lexical */ | ||
source: string | 'Lexical'; | ||
/** The version of Lexical that produced this document */ | ||
version: string; | ||
} | ||
/** | ||
* Generates a SerializedDocument from the given EditorState | ||
* @param editorState - the EditorState to serialize | ||
* @param config - An object that optionally contains source and lastSaved. | ||
* source defaults to Lexical and lastSaved defaults to the current time in | ||
* epoch milliseconds. | ||
*/ | ||
export declare function serializedDocumentFromEditorState(editorState: EditorState, config?: Readonly<{ | ||
source?: string; | ||
lastSaved?: number; | ||
}>): SerializedDocument; | ||
/** | ||
* Parse an EditorState from the given editor and document | ||
* | ||
* @param editor - The lexical editor | ||
* @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed) | ||
*/ | ||
export declare function editorStateFromSerializedDocument(editor: LexicalEditor, maybeStringifiedDocument: SerializedDocument | string): EditorState; | ||
/** | ||
* Takes a file and inputs its content into the editor state as an input field. | ||
@@ -19,3 +47,3 @@ * @param editor - The lexical editor. | ||
* @param config - An object that optionally contains fileName and source. fileName defaults to | ||
* the current date (as a string) and source defaults to lexical. | ||
* the current date (as a string) and source defaults to Lexical. | ||
*/ | ||
@@ -22,0 +50,0 @@ export declare function exportFile(editor: LexicalEditor, config?: Readonly<{ |
@@ -8,3 +8,2 @@ /** | ||
*/ | ||
import { exportFile, importFile } from './fileImportExport'; | ||
export { exportFile, importFile }; | ||
export { editorStateFromSerializedDocument, exportFile, importFile, type SerializedDocument, serializedDocumentFromEditorState, } from './fileImportExport'; |
@@ -13,3 +13,3 @@ /** | ||
var version = "0.15.0"; | ||
var version = "0.16.0"; | ||
@@ -24,4 +24,30 @@ /** | ||
/** | ||
* Generates a SerializedDocument from the given EditorState | ||
* @param editorState - the EditorState to serialize | ||
* @param config - An object that optionally contains source and lastSaved. | ||
* source defaults to Lexical and lastSaved defaults to the current time in | ||
* epoch milliseconds. | ||
*/ | ||
function serializedDocumentFromEditorState(editorState, config = Object.freeze({})) { | ||
return { | ||
editorState: editorState.toJSON(), | ||
lastSaved: config.lastSaved || Date.now(), | ||
source: config.source || 'Lexical', | ||
version | ||
}; | ||
} | ||
/** | ||
* Parse an EditorState from the given editor and document | ||
* | ||
* @param editor - The lexical editor | ||
* @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed) | ||
*/ | ||
function editorStateFromSerializedDocument(editor, maybeStringifiedDocument) { | ||
const json = typeof maybeStringifiedDocument === 'string' ? JSON.parse(maybeStringifiedDocument) : maybeStringifiedDocument; | ||
return editor.parseEditorState(json.editorState); | ||
} | ||
/** | ||
* Takes a file and inputs its content into the editor state as an input field. | ||
@@ -32,5 +58,3 @@ * @param editor - The lexical editor. | ||
readTextFileFromSystem(text => { | ||
const json = JSON.parse(text); | ||
const editorState = editor.parseEditorState(JSON.stringify(json.editorState)); | ||
editor.setEditorState(editorState); | ||
editor.setEditorState(editorStateFromSerializedDocument(editor, text)); | ||
editor.dispatchCommand(lexical.CLEAR_HISTORY_COMMAND, undefined); | ||
@@ -59,2 +83,3 @@ }); | ||
} | ||
/** | ||
@@ -64,15 +89,12 @@ * Generates a .lexical file to be downloaded by the browser containing the current editor state. | ||
* @param config - An object that optionally contains fileName and source. fileName defaults to | ||
* the current date (as a string) and source defaults to lexical. | ||
* the current date (as a string) and source defaults to Lexical. | ||
*/ | ||
function exportFile(editor, config = Object.freeze({})) { | ||
const now = new Date(); | ||
const editorState = editor.getEditorState(); | ||
const documentJSON = { | ||
editorState: editorState, | ||
lastSaved: now.getTime(), | ||
source: config.source || 'Lexical', | ||
version | ||
}; | ||
const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), { | ||
...config, | ||
lastSaved: now.getTime() | ||
}); | ||
const fileName = config.fileName || now.toISOString(); | ||
exportBlob(documentJSON, `${fileName}.lexical`); | ||
exportBlob(serializedDocument, `${fileName}.lexical`); | ||
} | ||
@@ -101,3 +123,5 @@ | ||
exports.editorStateFromSerializedDocument = editorStateFromSerializedDocument; | ||
exports.exportFile = exportFile; | ||
exports.importFile = importFile; | ||
exports.serializedDocumentFromEditorState = serializedDocumentFromEditorState; |
@@ -9,4 +9,5 @@ /** | ||
'use strict';var f=require("lexical");function g(a){let c=document.createElement("input");c.type="file";c.accept=".lexical";c.addEventListener("change",b=>{b=b.target;if(b.files){b=b.files[0];let d=new FileReader;d.readAsText(b,"UTF-8");d.onload=e=>{e.target&&a(e.target.result)}}});c.click()} | ||
exports.exportFile=function(a,c=Object.freeze({})){var b=new Date;a={editorState:a.getEditorState(),lastSaved:b.getTime(),source:c.source||"Lexical",version:"0.15.0"};{c=`${c.fileName||b.toISOString()}.lexical`;b=document.createElement("a");let d=document.body;null!==d&&(d.appendChild(b),b.style.display="none",a=JSON.stringify(a),a=new Blob([a],{type:"octet/stream"}),a=window.URL.createObjectURL(a),b.href=a,b.download=c,b.click(),window.URL.revokeObjectURL(a),b.remove())}}; | ||
exports.importFile=function(a){g(c=>{c=JSON.parse(c);c=a.parseEditorState(JSON.stringify(c.editorState));a.setEditorState(c);a.dispatchCommand(f.CLEAR_HISTORY_COMMAND,void 0)})} | ||
'use strict';var e=require("lexical");function f(a,b=Object.freeze({})){return{editorState:a.toJSON(),lastSaved:b.lastSaved||Date.now(),source:b.source||"Lexical",version:"0.16.0"}}function h(a,b){b="string"===typeof b?JSON.parse(b):b;return a.parseEditorState(b.editorState)} | ||
function k(a){let b=document.createElement("input");b.type="file";b.accept=".lexical";b.addEventListener("change",c=>{c=c.target;if(c.files){c=c.files[0];let d=new FileReader;d.readAsText(c,"UTF-8");d.onload=g=>{g.target&&a(g.target.result)}}});b.click()}exports.editorStateFromSerializedDocument=h; | ||
exports.exportFile=function(a,b=Object.freeze({})){var c=new Date;a=f(a.getEditorState(),{...b,lastSaved:c.getTime()});{b=`${b.fileName||c.toISOString()}.lexical`;c=document.createElement("a");let d=document.body;null!==d&&(d.appendChild(c),c.style.display="none",a=JSON.stringify(a),a=new Blob([a],{type:"octet/stream"}),a=window.URL.createObjectURL(a),c.href=a,c.download=b,c.click(),window.URL.revokeObjectURL(a),c.remove())}}; | ||
exports.importFile=function(a){k(b=>{a.setEditorState(h(a,b));a.dispatchCommand(e.CLEAR_HISTORY_COMMAND,void 0)})};exports.serializedDocumentFromEditorState=f |
@@ -13,3 +13,3 @@ { | ||
"license": "MIT", | ||
"version": "0.15.0", | ||
"version": "0.16.0", | ||
"main": "LexicalFile.js", | ||
@@ -42,4 +42,4 @@ "types": "index.d.ts", | ||
"dependencies": { | ||
"lexical": "0.15.0" | ||
"lexical": "0.16.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
17109
336
3
+ Addedlexical@0.16.0(transitive)
- Removedlexical@0.15.0(transitive)
Updatedlexical@0.16.0