From 8d38459289fe684ec48ec536ec0f1f4d0ecfad2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 30 Nov 2020 09:21:31 +0100 Subject: [PATCH] refactoring --- packages/core/src/Editor.ts | 10 +++------- packages/core/src/utils/getMarkAttributes.ts | 4 +++- packages/core/src/utils/getNodeAttributes.ts | 4 +++- packages/core/src/utils/markHasAttributes.ts | 3 +-- packages/core/src/utils/markIsActive.ts | 5 +---- packages/core/src/utils/nodeIsActive.ts | 9 ++++++--- packages/core/src/utils/objectIncludes.ts | 5 +++++ 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 6aa49edb..58483131 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -12,11 +12,7 @@ import createStyleTag from './utils/createStyleTag' import CommandManager from './CommandManager' import ExtensionManager from './ExtensionManager' import EventEmitter from './EventEmitter' -import { - EditorOptions, - EditorContent, - CommandSpec, -} from './types' +import { EditorOptions, EditorContent, CommandSpec } from './types' import * as extensions from './extensions' import style from './style' @@ -332,7 +328,7 @@ export class Editor extends EventEmitter { * @param name Name of the node */ public getNodeAttributes(name: string) { - return getNodeAttributes(this.state, this.schema.nodes[name]) + return getNodeAttributes(this.state, name) } /** @@ -341,7 +337,7 @@ export class Editor extends EventEmitter { * @param name Name of the mark */ public getMarkAttributes(name: string) { - return getMarkAttributes(this.state, this.schema.marks[name]) + return getMarkAttributes(this.state, name) } /** diff --git a/packages/core/src/utils/getMarkAttributes.ts b/packages/core/src/utils/getMarkAttributes.ts index 95e250e3..828cef8d 100644 --- a/packages/core/src/utils/getMarkAttributes.ts +++ b/packages/core/src/utils/getMarkAttributes.ts @@ -1,7 +1,9 @@ import { EditorState } from 'prosemirror-state' import { Mark, MarkType } from 'prosemirror-model' +import getMarkType from './getMarkType' -export default function getMarkAttributes(state: EditorState, type: MarkType) { +export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType) { + const type = getMarkType(typeOrName, state.schema) const { from, to, empty } = state.selection let marks: Mark[] = [] diff --git a/packages/core/src/utils/getNodeAttributes.ts b/packages/core/src/utils/getNodeAttributes.ts index da861b51..bb42cee1 100644 --- a/packages/core/src/utils/getNodeAttributes.ts +++ b/packages/core/src/utils/getNodeAttributes.ts @@ -1,7 +1,9 @@ import { EditorState } from 'prosemirror-state' import { Node, NodeType } from 'prosemirror-model' +import getNodeType from './getNodeType' -export default function getNodeAttributes(state: EditorState, type: NodeType) { +export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType) { + const type = getNodeType(typeOrName, state.schema) const { from, to } = state.selection let nodes: Node[] = [] diff --git a/packages/core/src/utils/markHasAttributes.ts b/packages/core/src/utils/markHasAttributes.ts index 6db66894..0d8ba5f1 100644 --- a/packages/core/src/utils/markHasAttributes.ts +++ b/packages/core/src/utils/markHasAttributes.ts @@ -1,11 +1,10 @@ import { EditorState } from 'prosemirror-state' import { MarkType } from 'prosemirror-model' import getMarkAttributes from './getMarkAttributes' -import { AnyObject } from '../types' import isEmptyObject from './isEmptyObject' import objectIncludes from './objectIncludes' -export default function markHasAttributes(state: EditorState, type: MarkType, attributes: AnyObject) { +export default function markHasAttributes(state: EditorState, type: MarkType, attributes: {}) { if (isEmptyObject(attributes)) { return true } diff --git a/packages/core/src/utils/markIsActive.ts b/packages/core/src/utils/markIsActive.ts index c7d81a9f..2f7cebc5 100644 --- a/packages/core/src/utils/markIsActive.ts +++ b/packages/core/src/utils/markIsActive.ts @@ -1,7 +1,6 @@ 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, attributes = {}) { const { @@ -17,7 +16,5 @@ export default function markIsActive(state: EditorState, type: MarkType, attribu const hasAttributes = markHasAttributes(state, type, attributes) - return isEmptyObject(attributes) - ? hasMark - : hasMark && hasAttributes + return hasMark && hasAttributes } diff --git a/packages/core/src/utils/nodeIsActive.ts b/packages/core/src/utils/nodeIsActive.ts index 9f8dd83f..de1ed513 100644 --- a/packages/core/src/utils/nodeIsActive.ts +++ b/packages/core/src/utils/nodeIsActive.ts @@ -2,14 +2,17 @@ import { findParentNode, findSelectedNodeOfType } from 'prosemirror-utils' import { EditorState } from 'prosemirror-state' import { Node, NodeType } from 'prosemirror-model' -export default function nodeIsActive(state: EditorState, type: NodeType, attrs = {}) { +export default function nodeIsActive(state: EditorState, type: NodeType, attributes = {}) { const predicate = (node: Node) => node.type === type const node = findSelectedNodeOfType(type)(state.selection) || findParentNode(predicate)(state.selection) - if (!Object.keys(attrs).length || !node) { + if (!Object.keys(attributes).length || !node) { return !!node } - return node.node.hasMarkup(type, { ...node.node.attrs, ...attrs }) + return node.node.hasMarkup(type, { + ...node.node.attrs, + ...attributes, + }) } diff --git a/packages/core/src/utils/objectIncludes.ts b/packages/core/src/utils/objectIncludes.ts index a2853919..e8fef855 100644 --- a/packages/core/src/utils/objectIncludes.ts +++ b/packages/core/src/utils/objectIncludes.ts @@ -1,3 +1,8 @@ +/** + * Check if object1 includes object2 + * @param object1 Object + * @param object2 Object + */ export default function objectIncludes(object1: { [key: string ]: any }, object2: { [key: string ]: any }): boolean { return !!Object .keys(object2)