mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-12-13 07:42:23 +03:00
Lexical API: Added content module, testing and documented
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {$getRoot, $getSelection, LexicalEditor} from "lexical";
|
||||
import {$getRoot, $getSelection, $insertNodes, $isBlockElementNode, LexicalEditor} from "lexical";
|
||||
import {$generateHtmlFromNodes} from "@lexical/html";
|
||||
import {$htmlToBlockNodes} from "./nodes";
|
||||
import {$getNearestNodeBlockParent, $htmlToBlockNodes, $htmlToNodes} from "./nodes";
|
||||
|
||||
export function setEditorContentFromHtml(editor: LexicalEditor, html: string) {
|
||||
editor.update(() => {
|
||||
@@ -42,14 +42,34 @@ export function prependHtmlToEditor(editor: LexicalEditor, html: string) {
|
||||
export function insertHtmlIntoEditor(editor: LexicalEditor, html: string) {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
const nodes = $htmlToBlockNodes(editor, html);
|
||||
const nodes = $htmlToNodes(editor, html);
|
||||
|
||||
const reference = selection?.getNodes()[0];
|
||||
const referencesParents = reference?.getParents() || [];
|
||||
const topLevel = referencesParents[referencesParents.length - 1];
|
||||
if (topLevel && reference) {
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
reference.insertAfter(nodes[i]);
|
||||
let reference = selection?.getNodes()[0];
|
||||
let replacedReference = false;
|
||||
let parentBlock = reference ? $getNearestNodeBlockParent(reference) : null;
|
||||
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
const toInsert = nodes[i];
|
||||
if ($isBlockElementNode(toInsert) && parentBlock) {
|
||||
// Insert at a block level, before or after the referenced block
|
||||
// depending on if the reference has been replaced.
|
||||
if (replacedReference) {
|
||||
parentBlock.insertBefore(toInsert);
|
||||
} else {
|
||||
parentBlock.insertAfter(toInsert);
|
||||
}
|
||||
} else if ($isBlockElementNode(toInsert)) {
|
||||
// Otherwise append blocks to the root
|
||||
$getRoot().append(toInsert);
|
||||
} else if (!replacedReference) {
|
||||
// First inline node, replacing existing selection
|
||||
$insertNodes([toInsert]);
|
||||
reference = toInsert;
|
||||
parentBlock = $getNearestNodeBlockParent(reference);
|
||||
replacedReference = true;
|
||||
} else {
|
||||
// For other inline nodes, insert before the reference node
|
||||
reference?.insertBefore(toInsert)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user