From 337615f7feca5a5e979cbe225010ccf0a2818188 Mon Sep 17 00:00:00 2001 From: umaranis Date: Tue, 14 May 2024 18:38:27 +1000 Subject: [PATCH] feat: clear formatting command --- demos/playground/src/ToolbarPlayground.svelte | 2 + .../ClearFormattingDropDownItem.svelte | 83 +++++++++++++++++++ .../lib/core/plugins/DecoratorBlockNode.ts | 64 ++++++++++++++ packages/svelte-lexical/src/lib/index.ts | 1 + 4 files changed, 150 insertions(+) create mode 100644 packages/svelte-lexical/src/lib/components/toolbar/MoreStylesDropDown/ClearFormattingDropDownItem.svelte create mode 100644 packages/svelte-lexical/src/lib/core/plugins/DecoratorBlockNode.ts diff --git a/demos/playground/src/ToolbarPlayground.svelte b/demos/playground/src/ToolbarPlayground.svelte index 14e5a80..c433953 100644 --- a/demos/playground/src/ToolbarPlayground.svelte +++ b/demos/playground/src/ToolbarPlayground.svelte @@ -29,6 +29,7 @@ StrikethroughDropDownItem, SubscriptDropDownItem, SuperscriptDropDownItem, + ClearFormattingDropDownItem, } from 'svelte-lexical'; import InsertImageDialog from './InsertImageDialog.svelte'; @@ -69,6 +70,7 @@ + diff --git a/packages/svelte-lexical/src/lib/components/toolbar/MoreStylesDropDown/ClearFormattingDropDownItem.svelte b/packages/svelte-lexical/src/lib/components/toolbar/MoreStylesDropDown/ClearFormattingDropDownItem.svelte new file mode 100644 index 0000000..2df4b72 --- /dev/null +++ b/packages/svelte-lexical/src/lib/components/toolbar/MoreStylesDropDown/ClearFormattingDropDownItem.svelte @@ -0,0 +1,83 @@ + + + + + Clear Formatting + diff --git a/packages/svelte-lexical/src/lib/core/plugins/DecoratorBlockNode.ts b/packages/svelte-lexical/src/lib/core/plugins/DecoratorBlockNode.ts new file mode 100644 index 0000000..83e8074 --- /dev/null +++ b/packages/svelte-lexical/src/lib/core/plugins/DecoratorBlockNode.ts @@ -0,0 +1,64 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +import type { + ElementFormatType, + LexicalNode, + NodeKey, + SerializedLexicalNode, + Spread, +} from 'lexical'; + +import {DecoratorNode} from 'lexical'; + +export type SerializedDecoratorBlockNode = Spread< + { + format: ElementFormatType; + }, + SerializedLexicalNode +>; + +export class DecoratorBlockNode extends DecoratorNode { + __format: ElementFormatType; + + constructor(format?: ElementFormatType, key?: NodeKey) { + super(key); + this.__format = format || ''; + } + + exportJSON(): SerializedDecoratorBlockNode { + return { + format: this.__format || '', + type: 'decorator-block', + version: 1, + }; + } + + createDOM(): HTMLElement { + return document.createElement('div'); + } + + updateDOM(): false { + return false; + } + + setFormat(format: ElementFormatType): void { + const self = this.getWritable(); + self.__format = format; + } + + isInline(): false { + return false; + } +} + +export function $isDecoratorBlockNode( + node: LexicalNode | null | undefined, +): node is DecoratorBlockNode { + return node instanceof DecoratorBlockNode; +} diff --git a/packages/svelte-lexical/src/lib/index.ts b/packages/svelte-lexical/src/lib/index.ts index c3742e2..1d89340 100644 --- a/packages/svelte-lexical/src/lib/index.ts +++ b/packages/svelte-lexical/src/lib/index.ts @@ -66,6 +66,7 @@ export {default as MoreStylesDropDown} from './components/toolbar/MoreStylesDrop export {default as StrikethroughDropDownItem} from './components/toolbar/MoreStylesDropDown/StrikethroughDropDownItem.svelte'; export {default as SubscriptDropDownItem} from './components/toolbar/MoreStylesDropDown/SubscriptDropDownItem.svelte'; export {default as SuperscriptDropDownItem} from './components/toolbar/MoreStylesDropDown/SuperscriptDropDownItem.svelte'; +export {default as ClearFormattingDropDownItem} from './components/toolbar/MoreStylesDropDown/ClearFormattingDropDownItem.svelte'; // dialogs export {default as InsertImageDialog} from './components/toolbar/dialogs/InsertImageDialog.svelte'; export {default as InsertImageUploadedDialogBody} from './components/toolbar/dialogs/InsertImageUploadedDialogBody.svelte';