@ekz/lexical-headless 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.
- package/EkzLexicalHeadless.dev.js +40 -0
- package/EkzLexicalHeadless.dev.mjs +38 -0
- package/EkzLexicalHeadless.js +11 -0
- package/EkzLexicalHeadless.mjs +12 -0
- package/EkzLexicalHeadless.node.mjs +10 -0
- package/EkzLexicalHeadless.prod.js +9 -0
- package/EkzLexicalHeadless.prod.mjs +9 -0
- package/EkzLexicalHeadlessDom.browser.dev.js +32 -0
- package/EkzLexicalHeadlessDom.browser.dev.mjs +30 -0
- package/EkzLexicalHeadlessDom.browser.js +11 -0
- package/EkzLexicalHeadlessDom.browser.mjs +12 -0
- package/EkzLexicalHeadlessDom.browser.node.mjs +10 -0
- package/EkzLexicalHeadlessDom.browser.prod.js +9 -0
- package/EkzLexicalHeadlessDom.browser.prod.mjs +9 -0
- package/EkzLexicalHeadlessDom.dev.js +66 -0
- package/EkzLexicalHeadlessDom.dev.mjs +64 -0
- package/EkzLexicalHeadlessDom.js +11 -0
- package/EkzLexicalHeadlessDom.mjs +12 -0
- package/EkzLexicalHeadlessDom.node.mjs +10 -0
- package/EkzLexicalHeadlessDom.prod.js +9 -0
- package/EkzLexicalHeadlessDom.prod.mjs +9 -0
- package/LICENSE +21 -0
- package/LexicalHeadless.js.flow +25 -0
- package/LexicalHeadlessDom.js.flow +13 -0
- package/README.md +53 -0
- package/dom.browser.d.ts +1 -0
- package/dom.d.ts +20 -0
- package/dom.www.d.cts +1 -0
- package/index.d.ts +15 -0
- package/package.json +60 -0
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
/**
|
|
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
|
+
/**
|
|
23
|
+
* Generates a headless editor that allows lexical to be used without the need for a DOM, eg in Node.js.
|
|
24
|
+
* Throws an error when unsupported methods are used.
|
|
25
|
+
* @param editorConfig - The optional lexical editor configuration.
|
|
26
|
+
* @returns - The configured headless editor.
|
|
27
|
+
*/
|
|
28
|
+
function createHeadlessEditor(editorConfig) {
|
|
29
|
+
const editor = lexical.createEditor(editorConfig);
|
|
30
|
+
editor._headless = true;
|
|
31
|
+
const unsupportedMethods = ['registerDecoratorListener', 'registerRootListener', 'registerMutationListener', 'getRootElement', 'setRootElement', 'getElementByKey', 'focus', 'blur'];
|
|
32
|
+
unsupportedMethods.forEach(method => {
|
|
33
|
+
editor[method] = () => {
|
|
34
|
+
throw new Error(`${method} is not supported in headless mode`);
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
return editor;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.createHeadlessEditor = createHeadlessEditor;
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { createEditor } from '@ekz/lexical';
|
|
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
|
+
/**
|
|
21
|
+
* Generates a headless editor that allows lexical to be used without the need for a DOM, eg in Node.js.
|
|
22
|
+
* Throws an error when unsupported methods are used.
|
|
23
|
+
* @param editorConfig - The optional lexical editor configuration.
|
|
24
|
+
* @returns - The configured headless editor.
|
|
25
|
+
*/
|
|
26
|
+
function createHeadlessEditor(editorConfig) {
|
|
27
|
+
const editor = createEditor(editorConfig);
|
|
28
|
+
editor._headless = true;
|
|
29
|
+
const unsupportedMethods = ['registerDecoratorListener', 'registerRootListener', 'registerMutationListener', 'getRootElement', 'setRootElement', 'getElementByKey', 'focus', 'blur'];
|
|
30
|
+
unsupportedMethods.forEach(method => {
|
|
31
|
+
editor[method] = () => {
|
|
32
|
+
throw new Error(`${method} is not supported in headless mode`);
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
return editor;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { createHeadlessEditor };
|
|
@@ -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 EkzLexicalHeadless = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalHeadless.dev.js') : require('./EkzLexicalHeadless.prod.js');
|
|
11
|
+
module.exports = EkzLexicalHeadless;
|
|
@@ -0,0 +1,12 @@
|
|
|
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 './EkzLexicalHeadless.dev.mjs';
|
|
10
|
+
import * as modProd from './EkzLexicalHeadless.prod.mjs';
|
|
11
|
+
const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
|
|
12
|
+
export const createHeadlessEditor = mod.createHeadlessEditor;
|
|
@@ -0,0 +1,10 @@
|
|
|
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('./EkzLexicalHeadless.dev.mjs') : import('./EkzLexicalHeadless.prod.mjs'));
|
|
10
|
+
export const createHeadlessEditor = mod.createHeadlessEditor;
|
|
@@ -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");exports.createHeadlessEditor=function(t){const r=e.createEditor(t);return r._headless=!0,["registerDecoratorListener","registerRootListener","registerMutationListener","getRootElement","setRootElement","getElementByKey","focus","blur"].forEach(e=>{r[e]=()=>{throw new Error(`${e} is not supported in headless mode`)}}),r};
|
|
@@ -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{createEditor as e}from"@ekz/lexical";function t(t){const r=e(t);r._headless=!0;return["registerDecoratorListener","registerRootListener","registerMutationListener","getRootElement","setRootElement","getElementByKey","focus","blur"].forEach(e=>{r[e]=()=>{throw new Error(`${e} is not supported in headless mode`)}}),r}export{t as createHeadlessEditor};
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
/**
|
|
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
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
20
|
+
|
|
21
|
+
function formatDevErrorMessage(message) {
|
|
22
|
+
throw new Error(message);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function withDOM(f) {
|
|
26
|
+
if (!!!globalThis.window) {
|
|
27
|
+
formatDevErrorMessage(`@lexical/headless/dom compiled for browser used in an environment without a global window`);
|
|
28
|
+
}
|
|
29
|
+
return f(globalThis.window);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.withDOM = withDOM;
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
/**
|
|
10
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
11
|
+
*
|
|
12
|
+
* This source code is licensed under the MIT license found in the
|
|
13
|
+
* LICENSE file in the root directory of this source tree.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
18
|
+
|
|
19
|
+
function formatDevErrorMessage(message) {
|
|
20
|
+
throw new Error(message);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function withDOM(f) {
|
|
24
|
+
if (!!!globalThis.window) {
|
|
25
|
+
formatDevErrorMessage(`@lexical/headless/dom compiled for browser used in an environment without a global window`);
|
|
26
|
+
}
|
|
27
|
+
return f(globalThis.window);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { withDOM };
|
|
@@ -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 EkzLexicalHeadlessDom.browser = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalHeadlessDom.browser.dev.js') : require('./EkzLexicalHeadlessDom.browser.prod.js');
|
|
11
|
+
module.exports = EkzLexicalHeadlessDom.browser;
|
|
@@ -0,0 +1,12 @@
|
|
|
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 './EkzLexicalHeadlessDom.browser.dev.mjs';
|
|
10
|
+
import * as modProd from './EkzLexicalHeadlessDom.browser.prod.mjs';
|
|
11
|
+
const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
|
|
12
|
+
export const withDOM = mod.withDOM;
|
|
@@ -0,0 +1,10 @@
|
|
|
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('./EkzLexicalHeadlessDom.browser.dev.mjs') : import('./EkzLexicalHeadlessDom.browser.prod.mjs'));
|
|
10
|
+
export const withDOM = mod.withDOM;
|
|
@@ -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";exports.withDOM=function(r){return globalThis.window||function(r,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",r);for(const r of e)o.append("v",r);throw n.search=o.toString(),Error(`Minified Lexical error #${r}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(335),r(globalThis.window)};
|
|
@@ -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
|
+
function r(r){return globalThis.window||function(r,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",r);for(const r of e)o.append("v",r);throw n.search=o.toString(),Error(`Minified Lexical error #${r}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(335),r(globalThis.window)}export{r as withDOM};
|
|
@@ -0,0 +1,66 @@
|
|
|
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 happyDom = require('happy-dom');
|
|
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
|
+
function createWindow() {
|
|
22
|
+
// @ts-expect-error -- DOMWindow is not exactly Window
|
|
23
|
+
return new happyDom.Window();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Call the given synchronous function with a window object,
|
|
28
|
+
* either from the browser or happy-dom in a non-browser
|
|
29
|
+
* environment. It will also set window, document, and MutationObserver
|
|
30
|
+
* on globalThis while the callback is running. This is
|
|
31
|
+
* useful primarily to parse and render HTML server-side.
|
|
32
|
+
*
|
|
33
|
+
* It is not safe to do anything asynchronous during this callback.
|
|
34
|
+
*
|
|
35
|
+
* @param f A function that uses the window object
|
|
36
|
+
* @returns The result of that function.
|
|
37
|
+
*/
|
|
38
|
+
function withDOM(f) {
|
|
39
|
+
const prevWindow = globalThis.window;
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- handle recursive case
|
|
41
|
+
if (prevWindow) {
|
|
42
|
+
return f(globalThis.window);
|
|
43
|
+
}
|
|
44
|
+
const prevComputedStyle = globalThis.getComputedStyle;
|
|
45
|
+
const prevDOMParser = globalThis.DOMParser;
|
|
46
|
+
const prevMutationObserver = globalThis.MutationObserver;
|
|
47
|
+
const prevDocument = globalThis.document;
|
|
48
|
+
const newWindow = createWindow();
|
|
49
|
+
globalThis.window = newWindow;
|
|
50
|
+
globalThis.document = newWindow.document;
|
|
51
|
+
globalThis.MutationObserver = newWindow.MutationObserver;
|
|
52
|
+
globalThis.DOMParser = newWindow.DOMParser;
|
|
53
|
+
globalThis.getComputedStyle = newWindow.getComputedStyle;
|
|
54
|
+
try {
|
|
55
|
+
return f(newWindow);
|
|
56
|
+
} finally {
|
|
57
|
+
globalThis.getComputedStyle = prevComputedStyle;
|
|
58
|
+
globalThis.DOMParser = prevDOMParser;
|
|
59
|
+
globalThis.MutationObserver = prevMutationObserver;
|
|
60
|
+
globalThis.document = prevDocument;
|
|
61
|
+
globalThis.window = prevWindow;
|
|
62
|
+
newWindow.close();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.withDOM = withDOM;
|
|
@@ -0,0 +1,64 @@
|
|
|
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 { Window } from 'happy-dom';
|
|
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
|
+
function createWindow() {
|
|
20
|
+
// @ts-expect-error -- DOMWindow is not exactly Window
|
|
21
|
+
return new Window();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Call the given synchronous function with a window object,
|
|
26
|
+
* either from the browser or happy-dom in a non-browser
|
|
27
|
+
* environment. It will also set window, document, and MutationObserver
|
|
28
|
+
* on globalThis while the callback is running. This is
|
|
29
|
+
* useful primarily to parse and render HTML server-side.
|
|
30
|
+
*
|
|
31
|
+
* It is not safe to do anything asynchronous during this callback.
|
|
32
|
+
*
|
|
33
|
+
* @param f A function that uses the window object
|
|
34
|
+
* @returns The result of that function.
|
|
35
|
+
*/
|
|
36
|
+
function withDOM(f) {
|
|
37
|
+
const prevWindow = globalThis.window;
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- handle recursive case
|
|
39
|
+
if (prevWindow) {
|
|
40
|
+
return f(globalThis.window);
|
|
41
|
+
}
|
|
42
|
+
const prevComputedStyle = globalThis.getComputedStyle;
|
|
43
|
+
const prevDOMParser = globalThis.DOMParser;
|
|
44
|
+
const prevMutationObserver = globalThis.MutationObserver;
|
|
45
|
+
const prevDocument = globalThis.document;
|
|
46
|
+
const newWindow = createWindow();
|
|
47
|
+
globalThis.window = newWindow;
|
|
48
|
+
globalThis.document = newWindow.document;
|
|
49
|
+
globalThis.MutationObserver = newWindow.MutationObserver;
|
|
50
|
+
globalThis.DOMParser = newWindow.DOMParser;
|
|
51
|
+
globalThis.getComputedStyle = newWindow.getComputedStyle;
|
|
52
|
+
try {
|
|
53
|
+
return f(newWindow);
|
|
54
|
+
} finally {
|
|
55
|
+
globalThis.getComputedStyle = prevComputedStyle;
|
|
56
|
+
globalThis.DOMParser = prevDOMParser;
|
|
57
|
+
globalThis.MutationObserver = prevMutationObserver;
|
|
58
|
+
globalThis.document = prevDocument;
|
|
59
|
+
globalThis.window = prevWindow;
|
|
60
|
+
newWindow.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { withDOM };
|
|
@@ -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 EkzLexicalHeadlessDom = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalHeadlessDom.dev.js') : require('./EkzLexicalHeadlessDom.prod.js');
|
|
11
|
+
module.exports = EkzLexicalHeadlessDom;
|
|
@@ -0,0 +1,12 @@
|
|
|
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 './EkzLexicalHeadlessDom.dev.mjs';
|
|
10
|
+
import * as modProd from './EkzLexicalHeadlessDom.prod.mjs';
|
|
11
|
+
const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
|
|
12
|
+
export const withDOM = mod.withDOM;
|
|
@@ -0,0 +1,10 @@
|
|
|
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('./EkzLexicalHeadlessDom.dev.mjs') : import('./EkzLexicalHeadlessDom.prod.mjs'));
|
|
10
|
+
export const withDOM = mod.withDOM;
|
|
@@ -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 l=require("happy-dom");exports.withDOM=function(o){const e=globalThis.window;if(e)return o(globalThis.window);const t=globalThis.getComputedStyle,i=globalThis.DOMParser,s=globalThis.MutationObserver,a=globalThis.document,r=new l.Window;globalThis.window=r,globalThis.document=r.document,globalThis.MutationObserver=r.MutationObserver,globalThis.DOMParser=r.DOMParser,globalThis.getComputedStyle=r.getComputedStyle;try{return o(r)}finally{globalThis.getComputedStyle=t,globalThis.DOMParser=i,globalThis.MutationObserver=s,globalThis.document=a,globalThis.window=e,r.close()}};
|
|
@@ -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{Window as o}from"happy-dom";function l(l){const e=globalThis.window;if(e)return l(globalThis.window);const t=globalThis.getComputedStyle,i=globalThis.DOMParser,s=globalThis.MutationObserver,a=globalThis.document,r=new o;globalThis.window=r,globalThis.document=r.document,globalThis.MutationObserver=r.MutationObserver,globalThis.DOMParser=r.DOMParser,globalThis.getComputedStyle=r.getComputedStyle;try{return l(r)}finally{globalThis.getComputedStyle=t,globalThis.DOMParser=i,globalThis.MutationObserver=s,globalThis.document=a,globalThis.window=e,r.close()}}export{l as withDOM};
|
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,25 @@
|
|
|
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
|
+
import type {
|
|
11
|
+
LexicalEditor,
|
|
12
|
+
LexicalNode,
|
|
13
|
+
EditorState,
|
|
14
|
+
EditorThemeClasses,
|
|
15
|
+
} from '@ekz/lexical';
|
|
16
|
+
|
|
17
|
+
declare export function createHeadlessEditor(editorConfig?: {
|
|
18
|
+
editorState?: EditorState,
|
|
19
|
+
namespace?: string,
|
|
20
|
+
nodes?: $ReadOnlyArray<Class<LexicalNode>>,
|
|
21
|
+
onError?: (error: Error) => void,
|
|
22
|
+
parentEditor?: LexicalEditor,
|
|
23
|
+
editable?: boolean,
|
|
24
|
+
theme?: EditorThemeClasses,
|
|
25
|
+
}): LexicalEditor;
|
|
@@ -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
|
+
* @flow strict
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* LexicalHeadlessDom
|
|
12
|
+
*/
|
|
13
|
+
declare export function withDOM<T>(f: (window: typeof globalThis.window) => T): T;
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# `@lexical/headless`
|
|
2
|
+
|
|
3
|
+
[](https://lexical.dev/docs/api/modules/lexical_headless)
|
|
4
|
+
|
|
5
|
+
This package allows you to interact with Lexical in a headless environment (one that does not rely on DOM, e.g. for Node.js environment), and use its
|
|
6
|
+
main features like editor.update(), editor.registerNodeTransform(), editor.registerUpdateListener()
|
|
7
|
+
to create, update or traverse state.
|
|
8
|
+
|
|
9
|
+
Install `@lexical/headless`:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install --save @lexical/headless
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const { createHeadlessEditor } = require('@ekz/lexical-headless');
|
|
17
|
+
|
|
18
|
+
const editor = createHeadlessEditor({
|
|
19
|
+
nodes: [],
|
|
20
|
+
onError: () => {},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
editor.update(() => {
|
|
24
|
+
$getRoot().append(
|
|
25
|
+
$createParagraphNode().append(
|
|
26
|
+
$createTextNode('Hello world')
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Any plugins that do not rely on DOM could also be used. Here's an example of how
|
|
33
|
+
you can convert lexical editor state to markdown on server:
|
|
34
|
+
```js
|
|
35
|
+
const { createHeadlessEditor } = require('@ekz/lexical-headless');
|
|
36
|
+
const { $convertToMarkdownString, TRANSFORMERS } = require('@ekz/lexical-markdown');
|
|
37
|
+
|
|
38
|
+
app.get('article/:id/markdown', async (req, res) => {
|
|
39
|
+
const editor = createHeadlessEditor({
|
|
40
|
+
nodes: [],
|
|
41
|
+
onError: () => {},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const articleEditorStateJSON = await loadArticleBody(req.query.id);
|
|
45
|
+
editor.setEditorState(editor.parseEditorState(articleEditorStateJSON));
|
|
46
|
+
|
|
47
|
+
editor.update(() => {
|
|
48
|
+
const markdown = $convertToMarkdownString(TRANSFORMERS);
|
|
49
|
+
res.send(markdown);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
```
|
package/dom.browser.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function withDOM<T>(f: (window: Window) => T): T;
|
package/dom.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
* Call the given synchronous function with a window object,
|
|
10
|
+
* either from the browser or happy-dom in a non-browser
|
|
11
|
+
* environment. It will also set window, document, and MutationObserver
|
|
12
|
+
* on globalThis while the callback is running. This is
|
|
13
|
+
* useful primarily to parse and render HTML server-side.
|
|
14
|
+
*
|
|
15
|
+
* It is not safe to do anything asynchronous during this callback.
|
|
16
|
+
*
|
|
17
|
+
* @param f A function that uses the window object
|
|
18
|
+
* @returns The result of that function.
|
|
19
|
+
*/
|
|
20
|
+
export declare function withDOM<T>(f: (window: typeof globalThis.window) => T): T;
|
package/dom.www.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function withDOM(f: any): any;
|
package/index.d.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
|
+
import type { CreateEditorArgs, LexicalEditor } from '@ekz/lexical';
|
|
9
|
+
/**
|
|
10
|
+
* Generates a headless editor that allows lexical to be used without the need for a DOM, eg in Node.js.
|
|
11
|
+
* Throws an error when unsupported methods are used.
|
|
12
|
+
* @param editorConfig - The optional lexical editor configuration.
|
|
13
|
+
* @returns - The configured headless editor.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createHeadlessEditor(editorConfig?: CreateEditorArgs): LexicalEditor;
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ekz/lexical-headless",
|
|
3
|
+
"description": "This package contains Headless helpers and functionality for Lexical.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"lexical",
|
|
6
|
+
"editor",
|
|
7
|
+
"rich-text",
|
|
8
|
+
"headless"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"version": "0.40.0",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/facebook/lexical.git",
|
|
15
|
+
"directory": "packages/lexical-headless"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
"./dom": {
|
|
20
|
+
"browser": {
|
|
21
|
+
"types": "./dom.d.ts",
|
|
22
|
+
"development": "./LexicalHeadlessDom.browser.dev.mjs",
|
|
23
|
+
"production": "./LexicalHeadlessDom.browser.prod.mjs",
|
|
24
|
+
"default": "./LexicalHeadlessDom.browser.mjs"
|
|
25
|
+
},
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dom.d.ts",
|
|
28
|
+
"development": "./LexicalHeadlessDom.dev.mjs",
|
|
29
|
+
"production": "./LexicalHeadlessDom.prod.mjs",
|
|
30
|
+
"node": "./LexicalHeadlessDom.node.mjs",
|
|
31
|
+
"default": "./LexicalHeadlessDom.mjs"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dom.d.ts",
|
|
35
|
+
"development": "./LexicalHeadlessDom.dev.js",
|
|
36
|
+
"production": "./LexicalHeadlessDom.prod.js",
|
|
37
|
+
"default": "./LexicalHeadlessDom.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
".": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./index.d.ts",
|
|
43
|
+
"development": "./LexicalHeadless.dev.mjs",
|
|
44
|
+
"production": "./LexicalHeadless.prod.mjs",
|
|
45
|
+
"node": "./LexicalHeadless.node.mjs",
|
|
46
|
+
"default": "./LexicalHeadless.mjs"
|
|
47
|
+
},
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./index.d.ts",
|
|
50
|
+
"development": "./LexicalHeadless.dev.js",
|
|
51
|
+
"production": "./LexicalHeadless.prod.js",
|
|
52
|
+
"default": "./LexicalHeadless.js"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"happy-dom": "^20.0.0",
|
|
58
|
+
"@ekz/lexical": "0.40.0"
|
|
59
|
+
}
|
|
60
|
+
}
|