From 9d99e9c9d0bd5c09b091b9cab422bf063b87bee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 18 Nov 2020 11:05:19 +0100 Subject: [PATCH 1/7] remove updateMarkAttributes --- docs/src/docPages/api/commands.md | 2 +- packages/core/src/commands/addMark.ts | 14 +++++++++-- packages/core/src/commands/toggleMark.ts | 2 +- .../core/src/commands/updateMarkAttributes.ts | 25 ------------------- packages/core/src/extensions/commands.ts | 7 +----- packages/extension-font-family/src/index.ts | 2 +- packages/extension-link/src/index.ts | 2 +- 7 files changed, 17 insertions(+), 37 deletions(-) delete mode 100644 packages/core/src/commands/updateMarkAttributes.ts diff --git a/docs/src/docPages/api/commands.md b/docs/src/docPages/api/commands.md index 7302b8fe..8a28b065 100644 --- a/docs/src/docPages/api/commands.md +++ b/docs/src/docPages/api/commands.md @@ -106,6 +106,7 @@ Have a look at all of the core commands listed below. They should give you a goo ### Nodes & Marks | Command | Description | | ----------------------- | --------------------------------------------------------- | +| .addMark() | Add a mark with new attributes. | | .clearNodes() | Normalize nodes to a simple paragraph. | | .extendMarkRange() | Extends the text selection to the current mark. | | .removeMark() | Remove a mark in the current selection. | @@ -118,7 +119,6 @@ Have a look at all of the core commands listed below. They should give you a goo | .toggleBlockType() | Toggle a node with another node. | | .toggleMark() | Toggle a mark on and off. | | .toggleWrap() | Wraps nodes in another node, or removes an existing wrap. | -| .updateMarkAttributes() | Update a mark with new attributes. | | .updateNodeAttributes() | Update attributes of a node. | ### Lists diff --git a/packages/core/src/commands/addMark.ts b/packages/core/src/commands/addMark.ts index 4770fcc0..38ff5f7f 100644 --- a/packages/core/src/commands/addMark.ts +++ b/packages/core/src/commands/addMark.ts @@ -1,14 +1,24 @@ import { MarkType } from 'prosemirror-model' import { Command } from '../types' import getMarkType from '../utils/getMarkType' +import getMarkAttributes from '../utils/getMarkAttributes' export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => { const { selection } = tr + const { from, to, empty } = selection const type = getMarkType(typeOrName, state.schema) - const { from, to } = selection + const oldAttributes = getMarkAttributes(state, type) + const newAttributes = { + ...oldAttributes, + ...attributes, + } if (dispatch) { - tr.addMark(from, to, type.create(attributes)) + if (empty) { + tr.addStoredMark(type.create(newAttributes)) + } else { + tr.addMark(from, to, type.create(newAttributes)) + } } return true diff --git a/packages/core/src/commands/toggleMark.ts b/packages/core/src/commands/toggleMark.ts index 60408688..b1154b21 100644 --- a/packages/core/src/commands/toggleMark.ts +++ b/packages/core/src/commands/toggleMark.ts @@ -12,7 +12,7 @@ export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ s && !markIsActive(state, type, attributes) if (attributes && hasMarkWithDifferentAttributes) { - return commands.updateMarkAttributes(type, attributes) + return commands.addMark(type, attributes) } return toggleMark(type, attributes)(state, dispatch) diff --git a/packages/core/src/commands/updateMarkAttributes.ts b/packages/core/src/commands/updateMarkAttributes.ts deleted file mode 100644 index 76073d54..00000000 --- a/packages/core/src/commands/updateMarkAttributes.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { MarkType } from 'prosemirror-model' -import { Command } from '../types' -import getMarkType from '../utils/getMarkType' -import getMarkAttributes from '../utils/getMarkAttributes' - -export default (typeOrName: string | MarkType, attributes: {}): Command => ({ tr, state, dispatch }) => { - const { selection } = tr - const { from, to, empty } = selection - const type = getMarkType(typeOrName, state.schema) - const oldAttributes = getMarkAttributes(state, type) - const newAttributes = { - ...oldAttributes, - ...attributes, - } - - if (dispatch) { - if (empty) { - tr.addStoredMark(type.create(newAttributes)) - } else { - tr.addMark(from, to, type.create(newAttributes)) - } - } - - return true -} diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index 268d94ae..f10b2701 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -26,7 +26,6 @@ import toggleList from '../commands/toggleList' import toggleMark from '../commands/toggleMark' import toggleWrap from '../commands/toggleWrap' import tryCommand from '../commands/try' -import updateMarkAttributes from '../commands/updateMarkAttributes' import updateNodeAttributes from '../commands/updateNodeAttributes' import wrapInList from '../commands/wrapInList' @@ -34,7 +33,7 @@ export const Commands = Extension.create({ addCommands() { return { /** - * Add a mark. + * Add a mark with new attributes. */ addMark, /** @@ -141,10 +140,6 @@ export const Commands = Extension.create({ * Runs one command after the other and stops at the first which returns true. */ try: tryCommand, - /** - * Update a mark with new attributes. - */ - updateMarkAttributes, /** * Update attributes of a node. */ diff --git a/packages/extension-font-family/src/index.ts b/packages/extension-font-family/src/index.ts index e83846dc..c1eebf91 100644 --- a/packages/extension-font-family/src/index.ts +++ b/packages/extension-font-family/src/index.ts @@ -42,7 +42,7 @@ const FontFamily = Extension.create({ */ fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => { return chain() - .updateMarkAttributes('textStyle', { fontFamily }) + .addMark('textStyle', { fontFamily }) .removeEmptyTextStyle() .run() }, diff --git a/packages/extension-link/src/index.ts b/packages/extension-link/src/index.ts index f7607f1b..2b89b4c6 100644 --- a/packages/extension-link/src/index.ts +++ b/packages/extension-link/src/index.ts @@ -54,7 +54,7 @@ const Link = Mark.create({ return commands.removeMark('link') } - return commands.updateMarkAttributes('link', options) + return commands.addMark('link', options) }, } }, From 56c8bb1bd0fc8aea7f5c445a7868e2df18981eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 18 Nov 2020 11:10:06 +0100 Subject: [PATCH 2/7] add link commands --- docs/src/demos/Examples/Links/index.vue | 4 ++-- docs/src/demos/Marks/Link/index.vue | 4 ++-- packages/extension-link/src/index.ts | 22 +++++++++++++++------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/src/demos/Examples/Links/index.vue b/docs/src/demos/Examples/Links/index.vue index 6bb39c7d..d0a9be48 100644 --- a/docs/src/demos/Examples/Links/index.vue +++ b/docs/src/demos/Examples/Links/index.vue @@ -3,7 +3,7 @@ - @@ -55,7 +55,7 @@ export default { addLink() { const url = window.prompt('URL') - this.editor.chain().focus().link({ href: url }).run() + this.editor.chain().focus().addLink({ href: url }).run() }, }, diff --git a/docs/src/demos/Marks/Link/index.vue b/docs/src/demos/Marks/Link/index.vue index 296f6e8c..b3f13350 100644 --- a/docs/src/demos/Marks/Link/index.vue +++ b/docs/src/demos/Marks/Link/index.vue @@ -3,7 +3,7 @@ - @@ -52,7 +52,7 @@ export default { addLink() { const url = window.prompt('URL') - this.editor.chain().focus().link({ href: url }).run() + this.editor.chain().focus().addLink({ href: url }).run() }, }, diff --git a/packages/extension-link/src/index.ts b/packages/extension-link/src/index.ts index 2b89b4c6..829afd29 100644 --- a/packages/extension-link/src/index.ts +++ b/packages/extension-link/src/index.ts @@ -47,14 +47,22 @@ const Link = Mark.create({ addCommands() { return { /** - * Toggle or update a link mark + * Add a link mark */ - link: (options: { href?: string, target?: string } = {}): Command => ({ commands }) => { - if (!options.href) { - return commands.removeMark('link') - } - - return commands.addMark('link', options) + addLink: (attributes: { href?: string, target?: string } = {}): Command => ({ commands }) => { + return commands.addMark('link', attributes) + }, + /** + * Toggle a link mark + */ + toggleLink: (attributes: { href?: string, target?: string } = {}): Command => ({ commands }) => { + return commands.toggleMark('link', attributes) + }, + /** + * Remove a link mark + */ + removeLink: (): Command => ({ commands }) => { + return commands.removeMark('link') }, } }, From 56bb26f1247dbcc4634798b67a947d86405bb24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 18 Nov 2020 11:10:37 +0100 Subject: [PATCH 3/7] formatting --- packages/extension-bold/src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/extension-bold/src/index.ts b/packages/extension-bold/src/index.ts index 4e60e821..314fa873 100644 --- a/packages/extension-bold/src/index.ts +++ b/packages/extension-bold/src/index.ts @@ -1,5 +1,8 @@ import { - Command, Mark, markInputRule, markPasteRule, + Command, + Mark, + markInputRule, + markPasteRule, } from '@tiptap/core' export interface BoldOptions { From 6ca1a6c3075e8e9acd158eb01ba0fd79f3d46041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 18 Nov 2020 11:33:33 +0100 Subject: [PATCH 4/7] rename bulletlist command --- docs/src/demos/Examples/Basic/index.vue | 2 +- docs/src/demos/Examples/CollaborativeEditing/index.vue | 2 +- docs/src/demos/Examples/CollaborativeEditingWs/index.vue | 2 +- docs/src/demos/Nodes/BulletList/index.vue | 2 +- docs/src/demos/Nodes/ListItem/index.vue | 2 +- docs/src/docPages/api/keyboard-shortcuts.md | 2 +- docs/src/docPages/guide/build-custom-extensions.md | 4 ++-- packages/extension-bullet-list/src/index.ts | 4 ++-- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/src/demos/Examples/Basic/index.vue b/docs/src/demos/Examples/Basic/index.vue index 76450865..e67273d3 100644 --- a/docs/src/demos/Examples/Basic/index.vue +++ b/docs/src/demos/Examples/Basic/index.vue @@ -40,7 +40,7 @@ - - -