@lexical/file 0.13.0 → 0.14.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.esm.js +91 -0
- package/LexicalFile.dev.js +1 -1
- package/LexicalFile.esm.js +11 -0
- package/LexicalFile.js +1 -1
- package/LexicalFile.prod.esm.js +7 -0
- package/LexicalFile.prod.js +1 -1
- package/README.md +2 -0
- package/package.json +5 -3
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
import { CLEAR_HISTORY_COMMAND } from 'lexical';
|
|
8
|
+
|
|
9
|
+
var version = "0.14.0";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
+
*
|
|
14
|
+
* This source code is licensed under the MIT license found in the
|
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Takes a file and inputs its content into the editor state as an input field.
|
|
21
|
+
* @param editor - The lexical editor.
|
|
22
|
+
*/
|
|
23
|
+
function importFile(editor) {
|
|
24
|
+
readTextFileFromSystem(text => {
|
|
25
|
+
const json = JSON.parse(text);
|
|
26
|
+
const editorState = editor.parseEditorState(JSON.stringify(json.editorState));
|
|
27
|
+
editor.setEditorState(editorState);
|
|
28
|
+
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function readTextFileFromSystem(callback) {
|
|
32
|
+
const input = document.createElement('input');
|
|
33
|
+
input.type = 'file';
|
|
34
|
+
input.accept = '.lexical';
|
|
35
|
+
input.addEventListener('change', event => {
|
|
36
|
+
const target = event.target;
|
|
37
|
+
if (target.files) {
|
|
38
|
+
const file = target.files[0];
|
|
39
|
+
const reader = new FileReader();
|
|
40
|
+
reader.readAsText(file, 'UTF-8');
|
|
41
|
+
reader.onload = readerEvent => {
|
|
42
|
+
if (readerEvent.target) {
|
|
43
|
+
const content = readerEvent.target.result;
|
|
44
|
+
callback(content);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
input.click();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generates a .lexical file to be downloaded by the browser containing the current editor state.
|
|
53
|
+
* @param editor - The lexical editor.
|
|
54
|
+
* @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.
|
|
56
|
+
*/
|
|
57
|
+
function exportFile(editor, config = Object.freeze({})) {
|
|
58
|
+
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
|
+
};
|
|
66
|
+
const fileName = config.fileName || now.toISOString();
|
|
67
|
+
exportBlob(documentJSON, `${fileName}.lexical`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Adapted from https://stackoverflow.com/a/19328891/2013580
|
|
71
|
+
function exportBlob(data, fileName) {
|
|
72
|
+
const a = document.createElement('a');
|
|
73
|
+
const body = document.body;
|
|
74
|
+
if (body === null) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
body.appendChild(a);
|
|
78
|
+
a.style.display = 'none';
|
|
79
|
+
const json = JSON.stringify(data);
|
|
80
|
+
const blob = new Blob([json], {
|
|
81
|
+
type: 'octet/stream'
|
|
82
|
+
});
|
|
83
|
+
const url = window.URL.createObjectURL(blob);
|
|
84
|
+
a.href = url;
|
|
85
|
+
a.download = fileName;
|
|
86
|
+
a.click();
|
|
87
|
+
window.URL.revokeObjectURL(url);
|
|
88
|
+
a.remove();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { exportFile, importFile };
|
package/LexicalFile.dev.js
CHANGED
|
@@ -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
|
+
import * as modDev from './LexicalFile.dev.esm.js';
|
|
8
|
+
import * as modProd from './LexicalFile.prod.esm.js';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
|
+
export const exportFile = mod.exportFile;
|
|
11
|
+
export const importFile = mod.importFile;
|
package/LexicalFile.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict'
|
|
8
|
-
const LexicalFile = process.env.NODE_ENV === 'development' ? require('./LexicalFile.dev.js') : require('./LexicalFile.prod.js')
|
|
8
|
+
const LexicalFile = process.env.NODE_ENV === 'development' ? require('./LexicalFile.dev.js') : require('./LexicalFile.prod.js');
|
|
9
9
|
module.exports = LexicalFile;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
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.0"},`${t.fileName||n.toISOString()}.lexical`)}export{n as exportFile,t as importFile};
|
package/LexicalFile.prod.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
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.
|
|
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.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())}};
|
|
9
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)})}
|
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -10,14 +10,16 @@
|
|
|
10
10
|
"export"
|
|
11
11
|
],
|
|
12
12
|
"license": "MIT",
|
|
13
|
-
"version": "0.
|
|
13
|
+
"version": "0.14.0",
|
|
14
14
|
"main": "LexicalFile.js",
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"lexical": "0.
|
|
16
|
+
"lexical": "0.14.0"
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
20
|
"url": "https://github.com/facebook/lexical",
|
|
21
21
|
"directory": "packages/lexical-file"
|
|
22
|
-
}
|
|
22
|
+
},
|
|
23
|
+
"module": "LexicalFile.esm.js",
|
|
24
|
+
"sideEffects": false
|
|
23
25
|
}
|