From 83ebbf957adb589c777cd18abc3db025c8b383ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 2 Nov 2020 15:46:56 +0100 Subject: [PATCH 1/2] add setDefaultNodeAttributes --- docs/src/demos/Extensions/TextAlign/index.vue | 3 ++ packages/core/src/extensions/index.ts | 1 + .../extensions/setDefaultNodeAttributes.ts | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 packages/core/src/extensions/setDefaultNodeAttributes.ts diff --git a/docs/src/demos/Extensions/TextAlign/index.vue b/docs/src/demos/Extensions/TextAlign/index.vue index 58af3f46..0771dfe7 100644 --- a/docs/src/demos/Extensions/TextAlign/index.vue +++ b/docs/src/demos/Extensions/TextAlign/index.vue @@ -9,6 +9,9 @@ + diff --git a/packages/core/src/extensions/index.ts b/packages/core/src/extensions/index.ts index d9006723..f21bbac3 100644 --- a/packages/core/src/extensions/index.ts +++ b/packages/core/src/extensions/index.ts @@ -11,6 +11,7 @@ export { RemoveMarks } from './removeMarks' export { ScrollIntoView } from './scrollIntoView' export { SelectAll } from './selectAll' export { SelectParentNode } from './selectParentNode' +export { SetDefaultNodeAttributes } from './setDefaultNodeAttributes' export { SetNodeAttributes } from './setNodeAttributes' export { SetBlockType } from './setBlockType' export { SetContent } from './setContent' diff --git a/packages/core/src/extensions/setDefaultNodeAttributes.ts b/packages/core/src/extensions/setDefaultNodeAttributes.ts new file mode 100644 index 00000000..8347b141 --- /dev/null +++ b/packages/core/src/extensions/setDefaultNodeAttributes.ts @@ -0,0 +1,36 @@ +import { Command } from '../Editor' +import { createExtension } from '../Extension' + +export const SetDefaultNodeAttributes = createExtension({ + addCommands() { + return { + setDefaultNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state }) => { + const { selection } = tr + const { from, to } = selection + + state.doc.nodesBetween(from, to, (node, pos) => { + if (!node.type.isText) { + attributeNames.forEach(name => { + const attribute = node.type.spec.attrs?.[name] + const defaultValue = attribute?.default + + if (attribute && defaultValue !== undefined) { + tr.setNodeMarkup(pos, undefined, { + [name]: defaultValue, + }) + } + }) + } + }) + + return true + }, + } + }, +}) + +declare module '../Editor' { + interface AllExtensions { + SetDefaultNodeAttributes: typeof SetDefaultNodeAttributes, + } +} From eed82a57b69f597654fb4cf5b64bdf5d716f39c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 2 Nov 2020 15:47:51 +0100 Subject: [PATCH 2/2] wording --- docs/src/demos/Extensions/TextAlign/index.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/demos/Extensions/TextAlign/index.vue b/docs/src/demos/Extensions/TextAlign/index.vue index 0771dfe7..25ddd3bf 100644 --- a/docs/src/demos/Extensions/TextAlign/index.vue +++ b/docs/src/demos/Extensions/TextAlign/index.vue @@ -10,7 +10,7 @@ right