From dc62ac1326bc3d114f868d22912005b73682fb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 6 Nov 2020 00:13:18 +0100 Subject: [PATCH] fix some bugs --- packages/core/src/commands/toggleMark.ts | 12 ++++++------ packages/core/src/utils/getMarkAttrs.ts | 5 +++-- packages/core/src/utils/markHasAttributes.ts | 11 ++++++++--- packages/core/src/utils/markIsActive.ts | 15 +++++++++------ packages/extension-highlight/index.ts | 8 ++------ 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/core/src/commands/toggleMark.ts b/packages/core/src/commands/toggleMark.ts index fc63b02b..b7cae680 100644 --- a/packages/core/src/commands/toggleMark.ts +++ b/packages/core/src/commands/toggleMark.ts @@ -4,16 +4,16 @@ import { Command } from '../Editor' import getMarkType from '../utils/getMarkType' import markIsActive from '../utils/markIsActive' -export default (typeOrName: string | MarkType, attrs?: {}): Command => ({ state, dispatch, commands }) => { +export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => { const type = getMarkType(typeOrName, state.schema) - const hasMarkWithDifferentAttributes = attrs + const hasMarkWithDifferentAttributes = attributes && markIsActive(state, type) - && !markIsActive(state, type, attrs) + && !markIsActive(state, type, attributes) - if (attrs && hasMarkWithDifferentAttributes) { - return commands.updateMarkAttributes(type, attrs) + if (attributes && hasMarkWithDifferentAttributes) { + return commands.updateMarkAttributes(type, attributes) } - return toggleMark(type)(state, dispatch) + return toggleMark(type, attributes)(state, dispatch) } diff --git a/packages/core/src/utils/getMarkAttrs.ts b/packages/core/src/utils/getMarkAttrs.ts index 3ab1842e..64719f95 100644 --- a/packages/core/src/utils/getMarkAttrs.ts +++ b/packages/core/src/utils/getMarkAttrs.ts @@ -2,10 +2,11 @@ import { EditorState } from 'prosemirror-state' import { Mark, MarkType } from 'prosemirror-model' export default function getMarkAttrs(state: EditorState, type: MarkType) { - const { from, to } = state.selection + const { from, to, empty } = state.selection let marks: Mark[] = [] - state.doc.nodesBetween(from, to, node => { + // TODO: -1 only for inclusive marks? + state.doc.nodesBetween(empty ? from - 1 : from, to, node => { marks = [...marks, ...node.marks] }) diff --git a/packages/core/src/utils/markHasAttributes.ts b/packages/core/src/utils/markHasAttributes.ts index 3ea639bb..23fbf37b 100644 --- a/packages/core/src/utils/markHasAttributes.ts +++ b/packages/core/src/utils/markHasAttributes.ts @@ -2,12 +2,17 @@ import { EditorState } from 'prosemirror-state' import { MarkType } from 'prosemirror-model' import getMarkAttrs from './getMarkAttrs' import { AnyObject } from '../types' +import isEmptyObject from './isEmptyObject' + +export default function markHasAttributes(state: EditorState, type: MarkType, attributes: AnyObject) { + if (isEmptyObject(attributes)) { + return true + } -export default function markHasAttributes(state: EditorState, type: MarkType, attrs: AnyObject) { const originalAttrs = getMarkAttrs(state, type) return !!Object - .keys(attrs) - .filter(key => attrs[key] === originalAttrs[key]) + .keys(attributes) + .filter(key => attributes[key] === originalAttrs[key]) .length } diff --git a/packages/core/src/utils/markIsActive.ts b/packages/core/src/utils/markIsActive.ts index 11fdba46..c7d81a9f 100644 --- a/packages/core/src/utils/markIsActive.ts +++ b/packages/core/src/utils/markIsActive.ts @@ -1,8 +1,9 @@ import { EditorState } from 'prosemirror-state' import { MarkType } from 'prosemirror-model' import markHasAttributes from './markHasAttributes' +import isEmptyObject from './isEmptyObject' -export default function markIsActive(state: EditorState, type: MarkType, attrs = {}) { +export default function markIsActive(state: EditorState, type: MarkType, attributes = {}) { const { from, $from, @@ -10,11 +11,13 @@ export default function markIsActive(state: EditorState, type: MarkType, attrs = empty, } = state.selection - const hasAttributes = markHasAttributes(state, type, attrs) + const hasMark = empty + ? !!(type.isInSet(state.storedMarks || $from.marks())) + : state.doc.rangeHasMark(from, to, type) - if (empty) { - return (type.isInSet(state.storedMarks || $from.marks()) && hasAttributes) - } + const hasAttributes = markHasAttributes(state, type, attributes) - return (state.doc.rangeHasMark(from, to, type) && hasAttributes) + return isEmptyObject(attributes) + ? hasMark + : hasMark && hasAttributes } diff --git a/packages/extension-highlight/index.ts b/packages/extension-highlight/index.ts index 5b8f89ee..a667d100 100644 --- a/packages/extension-highlight/index.ts +++ b/packages/extension-highlight/index.ts @@ -2,10 +2,6 @@ import { Command, createMark, markInputRule, markPasteRule, } from '@tiptap/core' -export interface HighlightOptions { - color: string, -} - export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm @@ -54,8 +50,8 @@ const Highlight = createMark({ addCommands() { return { - highlight: (attrs?: HighlightOptions): Command => ({ commands }) => { - return commands.toggleMark('highlight', attrs) + highlight: (attributes?: { color: string }): Command => ({ commands }) => { + return commands.toggleMark('highlight', attributes) }, } },