@lexical/file 0.44.1-nightly.20260519.0 → 0.45.1-dev.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.
@@ -10,7 +10,7 @@
10
10
 
11
11
  var lexical = require('lexical');
12
12
 
13
- var version = "0.44.1-nightly.20260519.0";
13
+ var version = "0.45.1-dev.0";
14
14
 
15
15
  /**
16
16
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -8,7 +8,7 @@
8
8
 
9
9
  import { CLEAR_HISTORY_COMMAND } from 'lexical';
10
10
 
11
- var version = "0.44.1-nightly.20260519.0";
11
+ var version = "0.45.1-dev.0";
12
12
 
13
13
  /**
14
14
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ "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.45.1-dev.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;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ 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.45.1-dev.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};
package/package.json CHANGED
@@ -10,34 +10,48 @@
10
10
  "export"
11
11
  ],
12
12
  "license": "MIT",
13
- "version": "0.44.1-nightly.20260519.0",
14
- "main": "LexicalFile.js",
15
- "types": "index.d.ts",
13
+ "version": "0.45.1-dev.0",
14
+ "main": "./dist/LexicalFile.js",
15
+ "types": "./dist/index.d.ts",
16
16
  "repository": {
17
17
  "type": "git",
18
18
  "url": "git+https://github.com/facebook/lexical.git",
19
19
  "directory": "packages/lexical-file"
20
20
  },
21
- "module": "LexicalFile.mjs",
21
+ "module": "./dist/LexicalFile.mjs",
22
22
  "sideEffects": false,
23
23
  "exports": {
24
24
  ".": {
25
+ "source": "./src/index.ts",
25
26
  "import": {
26
- "types": "./index.d.ts",
27
- "development": "./LexicalFile.dev.mjs",
28
- "production": "./LexicalFile.prod.mjs",
29
- "node": "./LexicalFile.node.mjs",
30
- "default": "./LexicalFile.mjs"
27
+ "types": "./dist/index.d.ts",
28
+ "development": "./dist/LexicalFile.dev.mjs",
29
+ "production": "./dist/LexicalFile.prod.mjs",
30
+ "node": "./dist/LexicalFile.node.mjs",
31
+ "default": "./dist/LexicalFile.mjs"
31
32
  },
32
33
  "require": {
33
- "types": "./index.d.ts",
34
- "development": "./LexicalFile.dev.js",
35
- "production": "./LexicalFile.prod.js",
36
- "default": "./LexicalFile.js"
34
+ "types": "./dist/index.d.ts",
35
+ "development": "./dist/LexicalFile.dev.js",
36
+ "production": "./dist/LexicalFile.prod.js",
37
+ "default": "./dist/LexicalFile.js"
37
38
  }
38
39
  }
39
40
  },
40
41
  "dependencies": {
41
- "lexical": "0.44.1-nightly.20260519.0"
42
- }
42
+ "lexical": "0.45.1-dev.0"
43
+ },
44
+ "files": [
45
+ "dist",
46
+ "src",
47
+ "!src/__tests__",
48
+ "!src/__bench__",
49
+ "!src/__mocks__",
50
+ "!src/**/*.test.ts",
51
+ "!src/**/*.test.tsx",
52
+ "!src/**/*.bench.ts",
53
+ "!src/**/*.bench.tsx",
54
+ "README.md",
55
+ "LICENSE"
56
+ ]
43
57
  }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import type {EditorState, LexicalEditor, SerializedEditorState} from 'lexical';
10
+
11
+ import {CLEAR_HISTORY_COMMAND} from 'lexical';
12
+
13
+ import {version} from '../package.json';
14
+
15
+ export interface SerializedDocument {
16
+ /** The serialized editorState produced by editorState.toJSON() */
17
+ editorState: SerializedEditorState;
18
+ /** The time this document was created in epoch milliseconds (Date.now()) */
19
+ lastSaved: number;
20
+ /** The source of the document, defaults to Lexical */
21
+ source: string | 'Lexical';
22
+ /** The version of Lexical that produced this document */
23
+ version: string;
24
+ }
25
+
26
+ /**
27
+ * Generates a SerializedDocument from the given EditorState
28
+ * @param editorState - the EditorState to serialize
29
+ * @param config - An object that optionally contains source and lastSaved.
30
+ * source defaults to Lexical and lastSaved defaults to the current time in
31
+ * epoch milliseconds.
32
+ */
33
+ export function serializedDocumentFromEditorState(
34
+ editorState: EditorState,
35
+ config: Readonly<{
36
+ source?: string;
37
+ lastSaved?: number;
38
+ }> = Object.freeze({}),
39
+ ): SerializedDocument {
40
+ return {
41
+ editorState: editorState.toJSON(),
42
+ lastSaved: config.lastSaved || Date.now(),
43
+ source: config.source || 'Lexical',
44
+ version,
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Parse an EditorState from the given editor and document
50
+ *
51
+ * @param editor - The lexical editor
52
+ * @param maybeStringifiedDocument - The contents of a .lexical file (as a JSON string, or already parsed)
53
+ */
54
+ export function editorStateFromSerializedDocument(
55
+ editor: LexicalEditor,
56
+ maybeStringifiedDocument: SerializedDocument | string,
57
+ ): EditorState {
58
+ const json =
59
+ typeof maybeStringifiedDocument === 'string'
60
+ ? JSON.parse(maybeStringifiedDocument)
61
+ : maybeStringifiedDocument;
62
+ return editor.parseEditorState(json.editorState);
63
+ }
64
+
65
+ /**
66
+ * Takes a file and inputs its content into the editor state as an input field.
67
+ * @param editor - The lexical editor.
68
+ */
69
+ export function importFile(editor: LexicalEditor) {
70
+ readTextFileFromSystem(text => {
71
+ editor.setEditorState(editorStateFromSerializedDocument(editor, text));
72
+ editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
73
+ });
74
+ }
75
+
76
+ function readTextFileFromSystem(callback: (text: string) => void) {
77
+ const input = document.createElement('input');
78
+ input.type = 'file';
79
+ input.accept = '.lexical';
80
+ input.addEventListener('change', (event: Event) => {
81
+ const target = event.target as HTMLInputElement;
82
+
83
+ if (target.files) {
84
+ const file = target.files[0];
85
+ const reader = new FileReader();
86
+ reader.readAsText(file, 'UTF-8');
87
+
88
+ reader.onload = readerEvent => {
89
+ if (readerEvent.target) {
90
+ const content = readerEvent.target.result;
91
+ callback(content as string);
92
+ }
93
+ };
94
+ }
95
+ });
96
+ input.click();
97
+ }
98
+
99
+ /**
100
+ * Generates a .lexical file to be downloaded by the browser containing the current editor state.
101
+ * @param editor - The lexical editor.
102
+ * @param config - An object that optionally contains fileName and source. fileName defaults to
103
+ * the current date (as a string) and source defaults to Lexical.
104
+ */
105
+ export function exportFile(
106
+ editor: LexicalEditor,
107
+ config: Readonly<{
108
+ fileName?: string;
109
+ source?: string;
110
+ }> = Object.freeze({}),
111
+ ) {
112
+ const now = new Date();
113
+ const serializedDocument = serializedDocumentFromEditorState(
114
+ editor.getEditorState(),
115
+ {
116
+ ...config,
117
+ lastSaved: now.getTime(),
118
+ },
119
+ );
120
+ const fileName = config.fileName || now.toISOString();
121
+ exportBlob(serializedDocument, `${fileName}.lexical`);
122
+ }
123
+
124
+ // Adapted from https://stackoverflow.com/a/19328891/2013580
125
+ function exportBlob(data: SerializedDocument, fileName: string) {
126
+ const a = document.createElement('a');
127
+ const body = document.body;
128
+
129
+ if (body === null) {
130
+ return;
131
+ }
132
+
133
+ body.appendChild(a);
134
+ a.style.display = 'none';
135
+ const json = JSON.stringify(data);
136
+ const blob = new Blob([json], {
137
+ type: 'octet/stream',
138
+ });
139
+ const url = window.URL.createObjectURL(blob);
140
+ a.href = url;
141
+ a.download = fileName;
142
+ a.click();
143
+ window.URL.revokeObjectURL(url);
144
+ a.remove();
145
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ export {
10
+ editorStateFromSerializedDocument,
11
+ exportFile,
12
+ importFile,
13
+ type SerializedDocument,
14
+ serializedDocumentFromEditorState,
15
+ } from './fileImportExport';
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- "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.44.1-nightly.20260519.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 i=JSON.stringify(e),r=new Blob([i],{type:"octet/stream"}),a=window.URL.createObjectURL(r);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;
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- 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.44.1-nightly.20260519.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};
File without changes
File without changes
File without changes