Skip to content

Commit

Permalink
PoC works. Some tests broken
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanJablo committed Oct 26, 2022
1 parent 505641f commit c5990ae
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 222 deletions.
365 changes: 193 additions & 172 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"@babel/preset-flow": "^7.14.5",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.16.7",
"@playwright/test": "^1.22.1",
"@playwright/test": "^1.27.1",
"@rollup/plugin-alias": "^3.1.4",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^19.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-code/src/CodeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

// eslint-disable-next-line simple-import-sort/imports
import type {
BlockNode,
DOMConversionMap,
DOMConversionOutput,
EditorConfig,
LexicalNode,
NodeKey,
ParagraphNode,
RangeSelection,
SerializedElementNode,
Spread,
Expand Down Expand Up @@ -212,7 +212,7 @@ export class CodeNode extends ElementNode {
// Mutation
insertNewAfter(
selection: RangeSelection,
): null | ParagraphNode | CodeHighlightNode {
): null | BlockNode | CodeHighlightNode {
const children = this.getChildren();
const childrenLength = children.length;

Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-list/src/formatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
$isParagraphNode,
$isRangeSelection,
$isRootOrShadowRoot,
BlockNode,
DEPRECATED_$isGridSelection,
ElementNode,
LexicalEditor,
LexicalNode,
NodeKey,
ParagraphNode,
} from 'lexical';
import invariant from 'shared/invariant';

Expand Down Expand Up @@ -235,7 +235,7 @@ export function removeList(editor: LexicalEditor): void {
}

for (const listNode of listNodes) {
let insertionPoint: ListNode | ParagraphNode = listNode;
let insertionPoint: ListNode | BlockNode = listNode;

const listItems = $getAllListItems(listNode);

Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-playground/src/nodes/PlaygroundNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
*/

import type {Klass, LexicalNode} from 'lexical';

import {CodeHighlightNode, CodeNode} from '@lexical/code';
import {HashtagNode} from '@lexical/hashtag';
import {AutoLinkNode, LinkNode} from '@lexical/link';
Expand All @@ -17,6 +15,7 @@ import {OverflowNode} from '@lexical/overflow';
import {HorizontalRuleNode} from '@lexical/react/LexicalHorizontalRuleNode';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {TableCellNode, TableNode, TableRowNode} from '@lexical/table';
import {BlockNode, Klass, LexicalNode} from 'lexical';

import {CollapsibleContainerNode} from '../plugins/CollapsiblePlugin/CollapsibleContainerNode';
import {CollapsibleContentNode} from '../plugins/CollapsiblePlugin/CollapsibleContentNode';
Expand All @@ -37,6 +36,7 @@ import {YouTubeNode} from './YouTubeNode';

const PlaygroundNodes: Array<Klass<LexicalNode>> = [
HeadingNode,
BlockNode,
ListNode,
ListItemNode,
QuoteNode,
Expand Down
22 changes: 9 additions & 13 deletions packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
*/

import type {LexicalEditor, NodeKey} from 'lexical';

import {
$createCodeNode,
$isCodeNode,
Expand All @@ -28,17 +26,13 @@ import {INSERT_EMBED_COMMAND} from '@lexical/react/LexicalAutoEmbedPlugin';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {$isDecoratorBlockNode} from '@lexical/react/LexicalDecoratorBlockNode';
import {INSERT_HORIZONTAL_RULE_COMMAND} from '@lexical/react/LexicalHorizontalRuleNode';
import {
$createHeadingNode,
$createQuoteNode,
$isHeadingNode,
HeadingTagType,
} from '@lexical/rich-text';
import {$createQuoteNode, HeadingTagType} from '@lexical/rich-text';
import {
$getSelectionStyleValueForProperty,
$isParentElementRTL,
$patchStyleText,
$selectAll,
$setBlocksType,
$wrapNodes,
} from '@lexical/selection';
import {
Expand All @@ -48,10 +42,10 @@ import {
mergeRegister,
} from '@lexical/utils';
import {
$createParagraphNode,
$getNodeByKey,
$getRoot,
$getSelection,
$isBlockNode,
$isRangeSelection,
$isRootOrShadowRoot,
$isTextNode,
Expand All @@ -62,6 +56,8 @@ import {
FORMAT_ELEMENT_COMMAND,
FORMAT_TEXT_COMMAND,
INDENT_CONTENT_COMMAND,
LexicalEditor,
NodeKey,
OUTDENT_CONTENT_COMMAND,
REDO_COMMAND,
SELECTION_CHANGE_COMMAND,
Expand Down Expand Up @@ -165,7 +161,7 @@ function BlockFormatDropDown({
$isRangeSelection(selection) ||
DEPRECATED_$isGridSelection(selection)
) {
$wrapNodes(selection, () => $createParagraphNode());
$setBlocksType(selection, 'paragraph');
}
});
}
Expand All @@ -180,7 +176,7 @@ function BlockFormatDropDown({
$isRangeSelection(selection) ||
DEPRECATED_$isGridSelection(selection)
) {
$wrapNodes(selection, () => $createHeadingNode(headingSize));
$setBlocksType(selection, headingSize);
}
});
}
Expand Down Expand Up @@ -448,8 +444,8 @@ export default function ToolbarPlugin(): JSX.Element {
: element.getListType();
setBlockType(type);
} else {
const type = $isHeadingNode(element)
? element.getTag()
const type = $isBlockNode(element)
? element.getType2()
: element.getType();
if (type in blockTypeToBlockName) {
setBlockType(type as keyof typeof blockTypeToBlockName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ export default function LexicalTableOfContentsPlugin({
const newHeading = $getNodeByKey<HeadingNode>(nodeKey);
if (newHeading !== null) {
let prevHeading = newHeading.getPreviousSibling();
while (prevHeading && !$isHeadingNode(prevHeading)) {
while (prevHeading && !(prevHeading instanceof HeadingNode)) {
// !$isHeadingNode(prevHeading)) {
prevHeading = prevHeading.getPreviousSibling();
}
currentTableOfContents = $insertHeadingIntoTableOfContents(
Expand Down Expand Up @@ -160,7 +161,8 @@ export default function LexicalTableOfContentsPlugin({
const currNode = $getNodeByKey(nodeKey);
if (currNode !== null) {
const parentNode = currNode.getParentOrThrow();
if ($isHeadingNode(parentNode)) {
if (parentNode instanceof HeadingNode) {
//$isHeadingNode(parentNode)) {
currentTableOfContents = $updateHeadingInTableOfContents(
parentNode,
currentTableOfContents,
Expand Down
30 changes: 16 additions & 14 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
LexicalEditor,
LexicalNode,
NodeKey,
ParagraphNode,
SerializedElementNode,
Spread,
TextFormatType,
Expand All @@ -36,6 +35,7 @@ import {
mergeRegister,
} from '@lexical/utils';
import {
$createBlockNode,
$createParagraphNode,
$getNearestNodeFromDOMNode,
$getSelection,
Expand All @@ -44,6 +44,7 @@ import {
$isRangeSelection,
$isRootNode,
$isTextNode,
BlockNode,
CLICK_COMMAND,
COMMAND_PRIORITY_EDITOR,
CONTROLLED_TEXT_INSERTION_COMMAND,
Expand Down Expand Up @@ -144,7 +145,7 @@ export class QuoteNode extends ElementNode {

// Mutation

insertNewAfter(): ParagraphNode {
insertNewAfter(): BlockNode {
const newBlock = $createParagraphNode();
const direction = this.getDirection();
newBlock.setDirection(direction);
Expand Down Expand Up @@ -267,13 +268,14 @@ export class HeadingNode extends ElementNode {
};
}

static importJSON(serializedNode: SerializedHeadingNode): HeadingNode {
const node = $createHeadingNode(serializedNode.tag);
node.setFormat(serializedNode.format);
node.setIndent(serializedNode.indent);
node.setDirection(serializedNode.direction);
return node;
}
// static importJSON(serializedNode: SerializedHeadingNode): HeadingNode {
// constimport { BlockNode } from './LexicalBlockNode';
// node = $createHeadingNode(serializedNode.tag);
// node.setFormat(serializedNode.format);
// node.setIndent(serializedNode.indent);
// node.setDirection(serializedNode.direction);
// return node;
// }

exportJSON(): SerializedHeadingNode {
return {
Expand All @@ -286,7 +288,7 @@ export class HeadingNode extends ElementNode {

// Mutation

insertNewAfter(): ParagraphNode {
insertNewAfter(): BlockNode {
const newElement = $createParagraphNode();
const direction = this.getDirection();
newElement.setDirection(direction);
Expand Down Expand Up @@ -335,14 +337,14 @@ function convertBlockquoteElement(): DOMConversionOutput {
return {node};
}

export function $createHeadingNode(headingTag: HeadingTagType): HeadingNode {
return new HeadingNode(headingTag);
export function $createHeadingNode(headingTag: HeadingTagType): BlockNode {
return $createBlockNode(headingTag);
}

export function $isHeadingNode(
node: LexicalNode | null | undefined,
): node is HeadingNode {
return node instanceof HeadingNode;
): node is BlockNode {
return node instanceof BlockNode;
}

function onPasteForRichText(
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-selection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
$moveCaretSelection,
$moveCharacter,
$selectAll,
$setBlocksType,
$shouldOverrideDefaultCharacterSelection,
$wrapNodes,
$wrapNodesImpl,
Expand All @@ -48,6 +49,7 @@ export {
$moveCaretSelection,
$moveCharacter,
$selectAll,
$setBlocksType,
$shouldOverrideDefaultCharacterSelection,
$wrapNodes,
$wrapNodesImpl,
Expand Down
54 changes: 45 additions & 9 deletions packages/lexical-selection/src/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
*/

import type {ICloneSelectionContent} from './lexical-node';
import type {
ElementNode,
GridSelection,
LexicalNode,
NodeKey,
Point,
RangeSelection,
TextNode,
} from 'lexical';

import {
$createBlockNode,
$getDecoratorNode,
$getPreviousSelection,
$getRoot,
$hasAncestor,
$isBlockNode,
$isDecoratorNode,
$isElementNode,
$isLeafNode,
Expand All @@ -29,6 +23,14 @@ import {
$isRootOrShadowRoot,
$isTextNode,
$setSelection,
BlockType2,
ElementNode,
GridSelection,
LexicalNode,
NodeKey,
Point,
RangeSelection,
TextNode,
} from 'lexical';

import {$cloneWithProperties} from './lexical-node';
Expand Down Expand Up @@ -63,6 +65,40 @@ function isPointAttached(point: Point): boolean {
* @param wrappingElement
* @returns
*/

export function $setBlocksType(
selection: RangeSelection | GridSelection,

blockType: BlockType2,
): void {
if (selection.anchor.key === 'root') {
const element = $createBlockNode(blockType);
const root = $getRoot();
const firstChild = root.getFirstChild();
if (firstChild) {
firstChild.replace(element);
firstChild.getChildren().forEach((child: LexicalNode) => {
element.append(child);
});
} else root.append(element);
return;
}

const nodes = selection.getNodes();
if (selection.anchor.type === 'text') {
let firstBlock = selection.anchor.getNode().getParent() as LexicalNode;
firstBlock = (
firstBlock.isInline() ? firstBlock.getParent() : firstBlock
) as LexicalNode;
if (nodes.indexOf(firstBlock) === -1) nodes.push(firstBlock);
}
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (!$isBlockNode(node)) continue;
node.setType2(blockType);
}
}

export function $wrapNodes(
selection: RangeSelection | GridSelection,
createElement: () => ElementNode,
Expand Down
6 changes: 6 additions & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type {
RangeSelection,
TextPointType as TextPoint,
} from './LexicalSelection';
export type {BlockType2} from './nodes/LexicalBlockNode';
export type {
ElementFormatType,
SerializedElementNode,
Expand Down Expand Up @@ -139,6 +140,11 @@ export {
$setSelection,
} from './LexicalUtils';
export {VERSION} from './LexicalVersion';
export {
$createBlockNode,
$isBlockNode,
BlockNode,
} from './nodes/LexicalBlockNode';
export {$isDecoratorNode, DecoratorNode} from './nodes/LexicalDecoratorNode';
export {$isElementNode, ElementNode} from './nodes/LexicalElementNode';
export {
Expand Down

0 comments on commit c5990ae

Please sign in to comment.