@ekz/lexical-file 0.40.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.
@@ -0,0 +1,121 @@
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';
10
+
11
+ var lexical = require('@ekz/lexical');
12
+
13
+ var version = "0.40.0";
14
+
15
+ /**
16
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
17
+ *
18
+ * This source code is licensed under the MIT license found in the
19
+ * LICENSE file in the root directory of this source tree.
20
+ *
21
+ */
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
+
50
+ /**
51
+ * Takes a file and inputs its content into the editor state as an input field.
52
+ * @param editor - The lexical editor.
53
+ */
54
+ function importFile(editor) {
55
+ readTextFileFromSystem(text => {
56
+ editor.setEditorState(editorStateFromSerializedDocument(editor, text));
57
+ editor.dispatchCommand(lexical.CLEAR_HISTORY_COMMAND, undefined);
58
+ });
59
+ }
60
+ function readTextFileFromSystem(callback) {
61
+ const input = document.createElement('input');
62
+ input.type = 'file';
63
+ input.accept = '.lexical';
64
+ input.addEventListener('change', event => {
65
+ const target = event.target;
66
+ if (target.files) {
67
+ const file = target.files[0];
68
+ const reader = new FileReader();
69
+ reader.readAsText(file, 'UTF-8');
70
+ reader.onload = readerEvent => {
71
+ if (readerEvent.target) {
72
+ const content = readerEvent.target.result;
73
+ callback(content);
74
+ }
75
+ };
76
+ }
77
+ });
78
+ input.click();
79
+ }
80
+
81
+ /**
82
+ * Generates a .lexical file to be downloaded by the browser containing the current editor state.
83
+ * @param editor - The lexical editor.
84
+ * @param config - An object that optionally contains fileName and source. fileName defaults to
85
+ * the current date (as a string) and source defaults to Lexical.
86
+ */
87
+ function exportFile(editor, config = Object.freeze({})) {
88
+ const now = new Date();
89
+ const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
90
+ ...config,
91
+ lastSaved: now.getTime()
92
+ });
93
+ const fileName = config.fileName || now.toISOString();
94
+ exportBlob(serializedDocument, `${fileName}.lexical`);
95
+ }
96
+
97
+ // Adapted from https://stackoverflow.com/a/19328891/2013580
98
+ function exportBlob(data, fileName) {
99
+ const a = document.createElement('a');
100
+ const body = document.body;
101
+ if (body === null) {
102
+ return;
103
+ }
104
+ body.appendChild(a);
105
+ a.style.display = 'none';
106
+ const json = JSON.stringify(data);
107
+ const blob = new Blob([json], {
108
+ type: 'octet/stream'
109
+ });
110
+ const url = window.URL.createObjectURL(blob);
111
+ a.href = url;
112
+ a.download = fileName;
113
+ a.click();
114
+ window.URL.revokeObjectURL(url);
115
+ a.remove();
116
+ }
117
+
118
+ exports.editorStateFromSerializedDocument = editorStateFromSerializedDocument;
119
+ exports.exportFile = exportFile;
120
+ exports.importFile = importFile;
121
+ exports.serializedDocumentFromEditorState = serializedDocumentFromEditorState;
@@ -0,0 +1,116 @@
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 } from '@ekz/lexical';
10
+
11
+ var version = "0.40.0";
12
+
13
+ /**
14
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
15
+ *
16
+ * This source code is licensed under the MIT license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ */
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
+
48
+ /**
49
+ * Takes a file and inputs its content into the editor state as an input field.
50
+ * @param editor - The lexical editor.
51
+ */
52
+ function importFile(editor) {
53
+ readTextFileFromSystem(text => {
54
+ editor.setEditorState(editorStateFromSerializedDocument(editor, text));
55
+ editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
56
+ });
57
+ }
58
+ function readTextFileFromSystem(callback) {
59
+ const input = document.createElement('input');
60
+ input.type = 'file';
61
+ input.accept = '.lexical';
62
+ input.addEventListener('change', event => {
63
+ const target = event.target;
64
+ if (target.files) {
65
+ const file = target.files[0];
66
+ const reader = new FileReader();
67
+ reader.readAsText(file, 'UTF-8');
68
+ reader.onload = readerEvent => {
69
+ if (readerEvent.target) {
70
+ const content = readerEvent.target.result;
71
+ callback(content);
72
+ }
73
+ };
74
+ }
75
+ });
76
+ input.click();
77
+ }
78
+
79
+ /**
80
+ * Generates a .lexical file to be downloaded by the browser containing the current editor state.
81
+ * @param editor - The lexical editor.
82
+ * @param config - An object that optionally contains fileName and source. fileName defaults to
83
+ * the current date (as a string) and source defaults to Lexical.
84
+ */
85
+ function exportFile(editor, config = Object.freeze({})) {
86
+ const now = new Date();
87
+ const serializedDocument = serializedDocumentFromEditorState(editor.getEditorState(), {
88
+ ...config,
89
+ lastSaved: now.getTime()
90
+ });
91
+ const fileName = config.fileName || now.toISOString();
92
+ exportBlob(serializedDocument, `${fileName}.lexical`);
93
+ }
94
+
95
+ // Adapted from https://stackoverflow.com/a/19328891/2013580
96
+ function exportBlob(data, fileName) {
97
+ const a = document.createElement('a');
98
+ const body = document.body;
99
+ if (body === null) {
100
+ return;
101
+ }
102
+ body.appendChild(a);
103
+ a.style.display = 'none';
104
+ const json = JSON.stringify(data);
105
+ const blob = new Blob([json], {
106
+ type: 'octet/stream'
107
+ });
108
+ const url = window.URL.createObjectURL(blob);
109
+ a.href = url;
110
+ a.download = fileName;
111
+ a.click();
112
+ window.URL.revokeObjectURL(url);
113
+ a.remove();
114
+ }
115
+
116
+ export { editorStateFromSerializedDocument, exportFile, importFile, serializedDocumentFromEditorState };
@@ -0,0 +1,11 @@
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'
10
+ const EkzLexicalFile = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalFile.dev.js') : require('./EkzLexicalFile.prod.js');
11
+ module.exports = EkzLexicalFile;
@@ -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
+ import * as modDev from './EkzLexicalFile.dev.mjs';
10
+ import * as modProd from './EkzLexicalFile.prod.mjs';
11
+ const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
12
+ export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
13
+ export const exportFile = mod.exportFile;
14
+ export const importFile = mod.importFile;
15
+ export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
@@ -0,0 +1,13 @@
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
+ const mod = await (process.env.NODE_ENV !== 'production' ? import('./EkzLexicalFile.dev.mjs') : import('./EkzLexicalFile.prod.mjs'));
10
+ export const editorStateFromSerializedDocument = mod.editorStateFromSerializedDocument;
11
+ export const exportFile = mod.exportFile;
12
+ export const importFile = mod.importFile;
13
+ export const serializedDocumentFromEditorState = mod.serializedDocumentFromEditorState;
@@ -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("@ekz/lexical");function t(e,t=Object.freeze({})){return{editorState:e.toJSON(),lastSaved:t.lastSaved||Date.now(),source:t.source||"Lexical",version:"0.40.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"@ekz/lexical";function t(e,t=Object.freeze({})){return{editorState:e.toJSON(),lastSaved:t.lastSaved||Date.now(),source:t.source||"Lexical",version:"0.40.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
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
+ * @flow strict
8
+ *
9
+ */
10
+
11
+ import type {LexicalEditor} from '@ekz/lexical';
12
+
13
+ declare export function importFile(editor: LexicalEditor): void;
14
+ declare export function exportFile(
15
+ editor: LexicalEditor,
16
+ config?: $ReadOnly<{fileName?: string, source?: string}>,
17
+ ): void;
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # `@lexical/file`
2
+
3
+ [![See API Documentation](https://lexical.dev/img/see-api-documentation.svg)](https://lexical.dev/docs/api/modules/lexical_file)
4
+
5
+ This package contains the functionality for the file import/export feature of Lexical.
@@ -0,0 +1,51 @@
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
+ import type { EditorState, LexicalEditor, SerializedEditorState } from '@ekz/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;
37
+ /**
38
+ * Takes a file and inputs its content into the editor state as an input field.
39
+ * @param editor - The lexical editor.
40
+ */
41
+ export declare function importFile(editor: LexicalEditor): void;
42
+ /**
43
+ * Generates a .lexical file to be downloaded by the browser containing the current editor state.
44
+ * @param editor - The lexical editor.
45
+ * @param config - An object that optionally contains fileName and source. fileName defaults to
46
+ * the current date (as a string) and source defaults to Lexical.
47
+ */
48
+ export declare function exportFile(editor: LexicalEditor, config?: Readonly<{
49
+ fileName?: string;
50
+ source?: string;
51
+ }>): void;
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
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
+ export { editorStateFromSerializedDocument, exportFile, importFile, type SerializedDocument, serializedDocumentFromEditorState, } from './fileImportExport';
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@ekz/lexical-file",
3
+ "description": "This package provides the file import/export feature for Lexical.",
4
+ "keywords": [
5
+ "lexical",
6
+ "editor",
7
+ "rich-text",
8
+ "file",
9
+ "import",
10
+ "export"
11
+ ],
12
+ "license": "MIT",
13
+ "version": "0.40.0",
14
+ "main": "LexicalFile.js",
15
+ "types": "index.d.ts",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/facebook/lexical.git",
19
+ "directory": "packages/lexical-file"
20
+ },
21
+ "module": "LexicalFile.mjs",
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": {
25
+ "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"
31
+ },
32
+ "require": {
33
+ "types": "./index.d.ts",
34
+ "development": "./LexicalFile.dev.js",
35
+ "production": "./LexicalFile.prod.js",
36
+ "default": "./LexicalFile.js"
37
+ }
38
+ }
39
+ },
40
+ "dependencies": {
41
+ "@ekz/lexical": "0.40.0"
42
+ }
43
+ }