@lexical/file 0.15.0 → 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.
- package/LexicalFile.dev.js +37 -13
- package/LexicalFile.dev.mjs +36 -14
- package/LexicalFile.mjs +3 -1
- package/LexicalFile.node.mjs +3 -1
- package/LexicalFile.prod.js +4 -3
- package/LexicalFile.prod.mjs +1 -1
- package/fileImportExport.d.ts +30 -2
- package/index.d.ts +1 -2
- package/package.json +2 -2
package/LexicalFile.dev.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
var lexical = require('lexical');
|
|
12
12
|
|
|
13
|
-
var version = "0.
|
|
13
|
+
var version = "0.16.0";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -20,6 +20,32 @@ var version = "0.15.0";
|
|
|
20
20
|
*
|
|
21
21
|
*/
|
|
22
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
|
+
}
|
|
23
49
|
|
|
24
50
|
/**
|
|
25
51
|
* Takes a file and inputs its content into the editor state as an input field.
|
|
@@ -27,9 +53,7 @@ var version = "0.15.0";
|
|
|
27
53
|
*/
|
|
28
54
|
function importFile(editor) {
|
|
29
55
|
readTextFileFromSystem(text => {
|
|
30
|
-
|
|
31
|
-
const editorState = editor.parseEditorState(JSON.stringify(json.editorState));
|
|
32
|
-
editor.setEditorState(editorState);
|
|
56
|
+
editor.setEditorState(editorStateFromSerializedDocument(editor, text));
|
|
33
57
|
editor.dispatchCommand(lexical.CLEAR_HISTORY_COMMAND, undefined);
|
|
34
58
|
});
|
|
35
59
|
}
|
|
@@ -53,23 +77,21 @@ function readTextFileFromSystem(callback) {
|
|
|
53
77
|
});
|
|
54
78
|
input.click();
|
|
55
79
|
}
|
|
80
|
+
|
|
56
81
|
/**
|
|
57
82
|
* Generates a .lexical file to be downloaded by the browser containing the current editor state.
|
|
58
83
|
* @param editor - The lexical editor.
|
|
59
84
|
* @param config - An object that optionally contains fileName and source. fileName defaults to
|
|
60
|
-
* the current date (as a string) and source defaults to
|
|
85
|
+
* the current date (as a string) and source defaults to Lexical.
|
|
61
86
|
*/
|
|
62
87
|
function exportFile(editor, config = Object.freeze({})) {
|
|
63
88
|
const now = new Date();
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
source: config.source || 'Lexical',
|
|
69
|
-
version
|
|
70
|
-
};
|
|
89
|
+
const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
|
|
90
|
+
...config,
|
|
91
|
+
lastSaved: now.getTime()
|
|
92
|
+
});
|
|
71
93
|
const fileName = config.fileName || now.toISOString();
|
|
72
|
-
exportBlob(
|
|
94
|
+
exportBlob(serializedDocument, `${fileName}.lexical`);
|
|
73
95
|
}
|
|
74
96
|
|
|
75
97
|
// Adapted from https://stackoverflow.com/a/19328891/2013580
|
|
@@ -93,5 +115,7 @@ function exportBlob(data, fileName) {
|
|
|
93
115
|
a.remove();
|
|
94
116
|
}
|
|
95
117
|
|
|
118
|
+
exports.editorStateFromSerializedDocument = editorStateFromSerializedDocument;
|
|
96
119
|
exports.exportFile = exportFile;
|
|
97
120
|
exports.importFile = importFile;
|
|
121
|
+
exports.serializedDocumentFromEditorState = serializedDocumentFromEditorState;
|
package/LexicalFile.dev.mjs
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { CLEAR_HISTORY_COMMAND } from 'lexical';
|
|
10
10
|
|
|
11
|
-
var version = "0.
|
|
11
|
+
var version = "0.16.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -18,6 +18,32 @@ var version = "0.15.0";
|
|
|
18
18
|
*
|
|
19
19
|
*/
|
|
20
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
|
+
}
|
|
21
47
|
|
|
22
48
|
/**
|
|
23
49
|
* Takes a file and inputs its content into the editor state as an input field.
|
|
@@ -25,9 +51,7 @@ var version = "0.15.0";
|
|
|
25
51
|
*/
|
|
26
52
|
function importFile(editor) {
|
|
27
53
|
readTextFileFromSystem(text => {
|
|
28
|
-
|
|
29
|
-
const editorState = editor.parseEditorState(JSON.stringify(json.editorState));
|
|
30
|
-
editor.setEditorState(editorState);
|
|
54
|
+
editor.setEditorState(editorStateFromSerializedDocument(editor, text));
|
|
31
55
|
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
|
|
32
56
|
});
|
|
33
57
|
}
|
|
@@ -51,23 +75,21 @@ function readTextFileFromSystem(callback) {
|
|
|
51
75
|
});
|
|
52
76
|
input.click();
|
|
53
77
|
}
|
|
78
|
+
|
|
54
79
|
/**
|
|
55
80
|
* Generates a .lexical file to be downloaded by the browser containing the current editor state.
|
|
56
81
|
* @param editor - The lexical editor.
|
|
57
82
|
* @param config - An object that optionally contains fileName and source. fileName defaults to
|
|
58
|
-
* the current date (as a string) and source defaults to
|
|
83
|
+
* the current date (as a string) and source defaults to Lexical.
|
|
59
84
|
*/
|
|
60
85
|
function exportFile(editor, config = Object.freeze({})) {
|
|
61
86
|
const now = new Date();
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
source: config.source || 'Lexical',
|
|
67
|
-
version
|
|
68
|
-
};
|
|
87
|
+
const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
|
|
88
|
+
...config,
|
|
89
|
+
lastSaved: now.getTime()
|
|
90
|
+
});
|
|
69
91
|
const fileName = config.fileName || now.toISOString();
|
|
70
|
-
exportBlob(
|
|
92
|
+
exportBlob(serializedDocument, `${fileName}.lexical`);
|
|
71
93
|
}
|
|
72
94
|
|
|
73
95
|
// Adapted from https://stackoverflow.com/a/19328891/2013580
|
|
@@ -91,4 +113,4 @@ function exportBlob(data, fileName) {
|
|
|
91
113
|
a.remove();
|
|
92
114
|
}
|
|
93
115
|
|
|
94
|
-
export { exportFile, importFile };
|
|
116
|
+
export { editorStateFromSerializedDocument, exportFile, importFile, serializedDocumentFromEditorState };
|
package/LexicalFile.mjs
CHANGED
|
@@ -9,5 +9,7 @@
|
|
|
9
9
|
import * as modDev from './LexicalFile.dev.mjs';
|
|
10
10
|
import * as modProd from './LexicalFile.prod.mjs';
|
|
11
11
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
12
|
+
export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
|
|
12
13
|
export const exportFile = mod.exportFile;
|
|
13
|
-
export const importFile = mod.importFile;
|
|
14
|
+
export const importFile = mod.importFile;
|
|
15
|
+
export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
|
package/LexicalFile.node.mjs
CHANGED
|
@@ -7,5 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalFile.dev.mjs') : import('./LexicalFile.prod.mjs'));
|
|
10
|
+
export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
|
|
10
11
|
export const exportFile = mod.exportFile;
|
|
11
|
-
export const importFile = mod.importFile;
|
|
12
|
+
export const importFile = mod.importFile;
|
|
13
|
+
export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
|
package/LexicalFile.prod.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
'use strict';var
|
|
10
|
-
|
|
11
|
-
exports.
|
|
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
|
package/LexicalFile.prod.mjs
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
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=>{
|
|
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};
|
package/fileImportExport.d.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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.
|
|
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.
|
|
41
|
+
"lexical": "0.16.0"
|
|
42
42
|
}
|
|
43
43
|
}
|