@lexical/headless 0.2.6
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/LICENSE +21 -0
- package/LexicalHeadless.d.ts +25 -0
- package/LexicalHeadless.dev.js +31 -0
- package/LexicalHeadless.js +9 -0
- package/LexicalHeadless.js.flow +26 -0
- package/LexicalHeadless.prod.js +7 -0
- package/README.md +45 -0
- package/package.json +21 -0
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
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
LexicalEditor,
|
|
11
|
+
LexicalNode,
|
|
12
|
+
EditorState,
|
|
13
|
+
EditorThemeClasses,
|
|
14
|
+
} from 'lexical';
|
|
15
|
+
|
|
16
|
+
export function createHeadlessEditor(editorConfig?: {
|
|
17
|
+
namespace?: string;
|
|
18
|
+
editorState?: EditorState;
|
|
19
|
+
theme?: EditorThemeClasses;
|
|
20
|
+
parentEditor?: LexicalEditor;
|
|
21
|
+
nodes?: $ReadOnlyArray<Class<LexicalNode>>;
|
|
22
|
+
onError: (error: Error) => void;
|
|
23
|
+
disableEvents?: boolean;
|
|
24
|
+
readOnly?: boolean;
|
|
25
|
+
}): LexicalEditor;
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var lexical = require('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
|
+
function createHeadlessEditor(editorConfig) {
|
|
20
|
+
const editor = lexical.createEditor(editorConfig);
|
|
21
|
+
editor._headless = true;
|
|
22
|
+
['registerDecoratorListener', 'registerRootListener', 'registerMutationListeners', 'getRootElement', 'setRootElement', 'getElementByKey', 'focus', 'blur'].forEach(method => {
|
|
23
|
+
// $FlowFixMe
|
|
24
|
+
editor[method] = () => {
|
|
25
|
+
throw new Error(`${method} is not supported in headless mode`);
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
return editor;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.createHeadlessEditor = 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
|
+
'use strict'
|
|
8
|
+
const LexicalHeadless = process.env.NODE_ENV === 'development' ? require('./LexicalHeadless.dev.js') : require('./LexicalHeadless.prod.js')
|
|
9
|
+
module.exports = LexicalHeadless;
|
|
@@ -0,0 +1,26 @@
|
|
|
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 'lexical';
|
|
16
|
+
|
|
17
|
+
declare export function createHeadlessEditor(editorConfig?: {
|
|
18
|
+
namespace?: string,
|
|
19
|
+
editorState?: EditorState,
|
|
20
|
+
theme?: EditorThemeClasses,
|
|
21
|
+
parentEditor?: LexicalEditor,
|
|
22
|
+
nodes?: $ReadOnlyArray<Class<LexicalNode>>,
|
|
23
|
+
onError: (error: Error) => void,
|
|
24
|
+
disableEvents?: boolean,
|
|
25
|
+
readOnly?: boolean,
|
|
26
|
+
}): LexicalEditor;
|
|
@@ -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
|
+
var c=require("lexical");exports.createHeadlessEditor=function(d){const a=c.createEditor(d);a._headless=!0;"registerDecoratorListener registerRootListener registerMutationListeners getRootElement setRootElement getElementByKey focus blur".split(" ").forEach(b=>{a[b]=()=>{throw Error(`${b} is not supported in headless mode`);}});return a};
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# `@lexical/headless`
|
|
2
|
+
|
|
3
|
+
This package allows creating headless lexical editor (that does not rely on DOM, e.g. for Node.js environment), and use its
|
|
4
|
+
main features like editor.update(), editor.registerNodeTransform(), editor.registerUpdateListener()
|
|
5
|
+
to create, update or traverse state.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const { createHeadlessEditor } = require('@lexical/headless');
|
|
9
|
+
|
|
10
|
+
const editor = createHeadlessEditor({
|
|
11
|
+
nodes: [],
|
|
12
|
+
onError: () => {},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
editor.update(() => {
|
|
16
|
+
$getRoot().append(
|
|
17
|
+
$createParagraphNode().append(
|
|
18
|
+
$createTextNode('Hello world')
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Any plugins that do not rely on DOM could also be used. Here's an example of how
|
|
25
|
+
you can convert lexical editor state to markdown on server:
|
|
26
|
+
```js
|
|
27
|
+
const { createHeadlessEditor } = require('@lexical/headless');
|
|
28
|
+
const { $convertToMarkdownString, TRANSFORMERS } = require('@lexical/markdown');
|
|
29
|
+
|
|
30
|
+
app.get('article/:id/markdown', await (req, res) => {
|
|
31
|
+
const editor = createHeadlessEditor({
|
|
32
|
+
nodes: [],
|
|
33
|
+
onError: () => {},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const articleEditorStateJSON = await loadArticleBody(req.query.id);
|
|
37
|
+
editor.setEditorState(editor.parseEditorState(articleEditorStateJSON));
|
|
38
|
+
|
|
39
|
+
editor.update(() => {
|
|
40
|
+
const markdown = $convertToMarkdownString(TRANSFORMERS);
|
|
41
|
+
res.send(markdown);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@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.2.6",
|
|
12
|
+
"main": "LexicalHeadless.js",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"lexical": "0.2.6"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/facebook/lexical",
|
|
19
|
+
"directory": "packages/lexical-headless"
|
|
20
|
+
}
|
|
21
|
+
}
|