@lexical/file 0.14.5 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,12 +3,14 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  'use strict';
8
10
 
9
11
  var lexical = require('lexical');
10
12
 
11
- var version = "0.14.5";
13
+ var version = "0.16.0";
12
14
 
13
15
  /**
14
16
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -18,15 +20,40 @@ var version = "0.14.5";
18
20
  *
19
21
  */
20
22
 
23
+ /**
24
+ * Generates a SerializedDocument from the given EditorState
25
+ * @param editorState - the EditorState to serialize
26
+ * @param config - An object that optionally contains source and lastSaved.
27
+ * source defaults to Lexical and lastSaved defaults to the current time in
28
+ * epoch milliseconds.
29
+ */
30
+ function serializedDocumentFromEditorState(editorState, config = Object.freeze({})) {
31
+ return {
32
+ editorState: editorState.toJSON(),
33
+ lastSaved: config.lastSaved || Date.now(),
34
+ source: config.source || 'Lexical',
35
+ version
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Parse an EditorState from the given editor and document
41
+ *
42
+ * @param editor - The lexical editor
43
+ * @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed)
44
+ */
45
+ function editorStateFromSerializedDocument(editor, maybeStringifiedDocument) {
46
+ const json = typeof maybeStringifiedDocument === 'string' ? JSON.parse(maybeStringifiedDocument) : maybeStringifiedDocument;
47
+ return editor.parseEditorState(json.editorState);
48
+ }
49
+
21
50
  /**
22
51
  * Takes a file and inputs its content into the editor state as an input field.
23
52
  * @param editor - The lexical editor.
24
53
  */
25
54
  function importFile(editor) {
26
55
  readTextFileFromSystem(text => {
27
- const json = JSON.parse(text);
28
- const editorState = editor.parseEditorState(JSON.stringify(json.editorState));
29
- editor.setEditorState(editorState);
56
+ editor.setEditorState(editorStateFromSerializedDocument(editor, text));
30
57
  editor.dispatchCommand(lexical.CLEAR_HISTORY_COMMAND, undefined);
31
58
  });
32
59
  }
@@ -50,23 +77,21 @@ function readTextFileFromSystem(callback) {
50
77
  });
51
78
  input.click();
52
79
  }
80
+
53
81
  /**
54
82
  * Generates a .lexical file to be downloaded by the browser containing the current editor state.
55
83
  * @param editor - The lexical editor.
56
84
  * @param config - An object that optionally contains fileName and source. fileName defaults to
57
- * the current date (as a string) and source defaults to lexical.
85
+ * the current date (as a string) and source defaults to Lexical.
58
86
  */
59
87
  function exportFile(editor, config = Object.freeze({})) {
60
88
  const now = new Date();
61
- const editorState = editor.getEditorState();
62
- const documentJSON = {
63
- editorState: editorState,
64
- lastSaved: now.getTime(),
65
- source: config.source || 'Lexical',
66
- version
67
- };
89
+ const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
90
+ ...config,
91
+ lastSaved: now.getTime()
92
+ });
68
93
  const fileName = config.fileName || now.toISOString();
69
- exportBlob(documentJSON, `${fileName}.lexical`);
94
+ exportBlob(serializedDocument, `${fileName}.lexical`);
70
95
  }
71
96
 
72
97
  // Adapted from https://stackoverflow.com/a/19328891/2013580
@@ -90,5 +115,7 @@ function exportBlob(data, fileName) {
90
115
  a.remove();
91
116
  }
92
117
 
118
+ exports.editorStateFromSerializedDocument = editorStateFromSerializedDocument;
93
119
  exports.exportFile = exportFile;
94
120
  exports.importFile = importFile;
121
+ exports.serializedDocumentFromEditorState = serializedDocumentFromEditorState;
@@ -3,10 +3,12 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  import { CLEAR_HISTORY_COMMAND } from 'lexical';
8
10
 
9
- var version = "0.14.5";
11
+ var version = "0.16.0";
10
12
 
11
13
  /**
12
14
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -16,15 +18,40 @@ var version = "0.14.5";
16
18
  *
17
19
  */
18
20
 
21
+ /**
22
+ * Generates a SerializedDocument from the given EditorState
23
+ * @param editorState - the EditorState to serialize
24
+ * @param config - An object that optionally contains source and lastSaved.
25
+ * source defaults to Lexical and lastSaved defaults to the current time in
26
+ * epoch milliseconds.
27
+ */
28
+ function serializedDocumentFromEditorState(editorState, config = Object.freeze({})) {
29
+ return {
30
+ editorState: editorState.toJSON(),
31
+ lastSaved: config.lastSaved || Date.now(),
32
+ source: config.source || 'Lexical',
33
+ version
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Parse an EditorState from the given editor and document
39
+ *
40
+ * @param editor - The lexical editor
41
+ * @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed)
42
+ */
43
+ function editorStateFromSerializedDocument(editor, maybeStringifiedDocument) {
44
+ const json = typeof maybeStringifiedDocument === 'string' ? JSON.parse(maybeStringifiedDocument) : maybeStringifiedDocument;
45
+ return editor.parseEditorState(json.editorState);
46
+ }
47
+
19
48
  /**
20
49
  * Takes a file and inputs its content into the editor state as an input field.
21
50
  * @param editor - The lexical editor.
22
51
  */
23
52
  function importFile(editor) {
24
53
  readTextFileFromSystem(text => {
25
- const json = JSON.parse(text);
26
- const editorState = editor.parseEditorState(JSON.stringify(json.editorState));
27
- editor.setEditorState(editorState);
54
+ editor.setEditorState(editorStateFromSerializedDocument(editor, text));
28
55
  editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
29
56
  });
30
57
  }
@@ -48,23 +75,21 @@ function readTextFileFromSystem(callback) {
48
75
  });
49
76
  input.click();
50
77
  }
78
+
51
79
  /**
52
80
  * Generates a .lexical file to be downloaded by the browser containing the current editor state.
53
81
  * @param editor - The lexical editor.
54
82
  * @param config - An object that optionally contains fileName and source. fileName defaults to
55
- * the current date (as a string) and source defaults to lexical.
83
+ * the current date (as a string) and source defaults to Lexical.
56
84
  */
57
85
  function exportFile(editor, config = Object.freeze({})) {
58
86
  const now = new Date();
59
- const editorState = editor.getEditorState();
60
- const documentJSON = {
61
- editorState: editorState,
62
- lastSaved: now.getTime(),
63
- source: config.source || 'Lexical',
64
- version
65
- };
87
+ const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
88
+ ...config,
89
+ lastSaved: now.getTime()
90
+ });
66
91
  const fileName = config.fileName || now.toISOString();
67
- exportBlob(documentJSON, `${fileName}.lexical`);
92
+ exportBlob(serializedDocument, `${fileName}.lexical`);
68
93
  }
69
94
 
70
95
  // Adapted from https://stackoverflow.com/a/19328891/2013580
@@ -88,4 +113,4 @@ function exportBlob(data, fileName) {
88
113
  a.remove();
89
114
  }
90
115
 
91
- export { exportFile, importFile };
116
+ export { editorStateFromSerializedDocument, exportFile, importFile, serializedDocumentFromEditorState };
package/LexicalFile.js CHANGED
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  'use strict'
8
10
  const LexicalFile = process.env.NODE_ENV === 'development' ? require('./LexicalFile.dev.js') : require('./LexicalFile.prod.js');
9
11
  module.exports = LexicalFile;
package/LexicalFile.mjs CHANGED
@@ -3,9 +3,13 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  import * as modDev from './LexicalFile.dev.mjs';
8
10
  import * as modProd from './LexicalFile.prod.mjs';
9
11
  const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
12
+ export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
10
13
  export const exportFile = mod.exportFile;
11
- export const importFile = mod.importFile;
14
+ export const importFile = mod.importFile;
15
+ export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
@@ -3,7 +3,11 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalFile.dev.mjs') : import('./LexicalFile.prod.mjs'));
10
+ export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
8
11
  export const exportFile = mod.exportFile;
9
- export const importFile = mod.importFile;
12
+ export const importFile = mod.importFile;
13
+ export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
@@ -3,7 +3,10 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
7
- '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()}
8
- exports.exportFile=function(a,c=Object.freeze({})){var b=new Date;a={editorState:a.getEditorState(),lastSaved:b.getTime(),source:c.source||"Lexical",version:"0.14.5"};{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())}};
9
- 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)})}
8
+
9
+ '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)}
10
+ 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;
11
+ 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())}};
12
+ exports.importFile=function(a){k(b=>{a.setEditorState(h(a,b));a.dispatchCommand(e.CLEAR_HISTORY_COMMAND,void 0)})};exports.serializedDocumentFromEditorState=f
@@ -3,5 +3,7 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
7
- import{CLEAR_HISTORY_COMMAND as e}from"lexical";function t(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()}((n=>{const o=JSON.parse(n),i=t.parseEditorState(JSON.stringify(o.editorState));t.setEditorState(i),t.dispatchCommand(e,void 0)}))}function n(e,t=Object.freeze({})){const n=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),c=new Blob([i],{type:"octet/stream"}),a=window.URL.createObjectURL(c);n.href=a,n.download=t,n.click(),window.URL.revokeObjectURL(a),n.remove()}({editorState:e.getEditorState(),lastSaved:n.getTime(),source:t.source||"Lexical",version:"0.14.5"},`${t.fileName||n.toISOString()}.lexical`)}export{n as exportFile,t as importFile};
8
+
9
+ import{CLEAR_HISTORY_COMMAND as e}from"lexical";var t="0.16.0";function n(e,n=Object.freeze({})){return{editorState:e.toJSON(),lastSaved:n.lastSaved||Date.now(),source:n.source||"Lexical",version:t}}function o(e,t){const n="string"==typeof t?JSON.parse(t):t;return e.parseEditorState(n.editorState)}function a(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()}((n=>{t.setEditorState(o(t,n)),t.dispatchCommand(e,void 0)}))}function i(e,t=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 a=JSON.stringify(e),i=new Blob([a],{type:"octet/stream"}),c=window.URL.createObjectURL(i);n.href=c,n.download=t,n.click(),window.URL.revokeObjectURL(c),n.remove()}(n(e.getEditorState(),{...t,lastSaved:o.getTime()}),`${t.fileName||o.toISOString()}.lexical`)}export{o as editorStateFromSerializedDocument,i as exportFile,a as importFile,n as serializedDocumentFromEditorState};
@@ -5,7 +5,35 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import type { LexicalEditor } from 'lexical';
8
+ import type { EditorState, LexicalEditor, SerializedEditorState } from 'lexical';
9
+ export interface SerializedDocument {
10
+ /** The serialized editorState produced by editorState.toJSON() */
11
+ editorState: SerializedEditorState;
12
+ /** The time this document was created in epoch milliseconds (Date.now()) */
13
+ lastSaved: number;
14
+ /** The source of the document, defaults to Lexical */
15
+ source: string | 'Lexical';
16
+ /** The version of Lexical that produced this document */
17
+ version: string;
18
+ }
19
+ /**
20
+ * Generates a SerializedDocument from the given EditorState
21
+ * @param editorState - the EditorState to serialize
22
+ * @param config - An object that optionally contains source and lastSaved.
23
+ * source defaults to Lexical and lastSaved defaults to the current time in
24
+ * epoch milliseconds.
25
+ */
26
+ export declare function serializedDocumentFromEditorState(editorState: EditorState, config?: Readonly<{
27
+ source?: string;
28
+ lastSaved?: number;
29
+ }>): SerializedDocument;
30
+ /**
31
+ * Parse an EditorState from the given editor and document
32
+ *
33
+ * @param editor - The lexical editor
34
+ * @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed)
35
+ */
36
+ export declare function editorStateFromSerializedDocument(editor: LexicalEditor, maybeStringifiedDocument: SerializedDocument | string): EditorState;
9
37
  /**
10
38
  * Takes a file and inputs its content into the editor state as an input field.
11
39
  * @param editor - The lexical editor.
@@ -15,7 +43,7 @@ export declare function importFile(editor: LexicalEditor): void;
15
43
  * Generates a .lexical file to be downloaded by the browser containing the current editor state.
16
44
  * @param editor - The lexical editor.
17
45
  * @param config - An object that optionally contains fileName and source. fileName defaults to
18
- * the current date (as a string) and source defaults to lexical.
46
+ * the current date (as a string) and source defaults to Lexical.
19
47
  */
20
48
  export declare function exportFile(editor: LexicalEditor, config?: Readonly<{
21
49
  fileName?: string;
package/index.d.ts CHANGED
@@ -5,5 +5,4 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import { exportFile, importFile } from './fileImportExport';
9
- export { exportFile, importFile };
8
+ export { editorStateFromSerializedDocument, exportFile, importFile, type SerializedDocument, serializedDocumentFromEditorState, } from './fileImportExport';
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "export"
11
11
  ],
12
12
  "license": "MIT",
13
- "version": "0.14.5",
13
+ "version": "0.16.0",
14
14
  "main": "LexicalFile.js",
15
15
  "types": "index.d.ts",
16
16
  "repository": {
@@ -38,6 +38,6 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "lexical": "0.14.5"
41
+ "lexical": "0.16.0"
42
42
  }
43
43
  }