refactoring

This commit is contained in:
Philipp Kühn
2020-11-30 09:21:31 +01:00
parent ec56158739
commit 8d38459289
7 changed files with 22 additions and 18 deletions

View File

@@ -12,11 +12,7 @@ import createStyleTag from './utils/createStyleTag'
import CommandManager from './CommandManager' import CommandManager from './CommandManager'
import ExtensionManager from './ExtensionManager' import ExtensionManager from './ExtensionManager'
import EventEmitter from './EventEmitter' import EventEmitter from './EventEmitter'
import { import { EditorOptions, EditorContent, CommandSpec } from './types'
EditorOptions,
EditorContent,
CommandSpec,
} from './types'
import * as extensions from './extensions' import * as extensions from './extensions'
import style from './style' import style from './style'
@@ -332,7 +328,7 @@ export class Editor extends EventEmitter {
* @param name Name of the node * @param name Name of the node
*/ */
public getNodeAttributes(name: string) { 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 * @param name Name of the mark
*/ */
public getMarkAttributes(name: string) { public getMarkAttributes(name: string) {
return getMarkAttributes(this.state, this.schema.marks[name]) return getMarkAttributes(this.state, name)
} }
/** /**

View File

@@ -1,7 +1,9 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model' 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 const { from, to, empty } = state.selection
let marks: Mark[] = [] let marks: Mark[] = []

View File

@@ -1,7 +1,9 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model' 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 const { from, to } = state.selection
let nodes: Node[] = [] let nodes: Node[] = []

View File

@@ -1,11 +1,10 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import getMarkAttributes from './getMarkAttributes' import getMarkAttributes from './getMarkAttributes'
import { AnyObject } from '../types'
import isEmptyObject from './isEmptyObject' import isEmptyObject from './isEmptyObject'
import objectIncludes from './objectIncludes' 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)) { if (isEmptyObject(attributes)) {
return true return true
} }

View File

@@ -1,7 +1,6 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import markHasAttributes from './markHasAttributes' import markHasAttributes from './markHasAttributes'
import isEmptyObject from './isEmptyObject'
export default function markIsActive(state: EditorState, type: MarkType, attributes = {}) { export default function markIsActive(state: EditorState, type: MarkType, attributes = {}) {
const { const {
@@ -17,7 +16,5 @@ export default function markIsActive(state: EditorState, type: MarkType, attribu
const hasAttributes = markHasAttributes(state, type, attributes) const hasAttributes = markHasAttributes(state, type, attributes)
return isEmptyObject(attributes) return hasMark && hasAttributes
? hasMark
: hasMark && hasAttributes
} }

View File

@@ -2,14 +2,17 @@ import { findParentNode, findSelectedNodeOfType } from 'prosemirror-utils'
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model' 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 predicate = (node: Node) => node.type === type
const node = findSelectedNodeOfType(type)(state.selection) const node = findSelectedNodeOfType(type)(state.selection)
|| findParentNode(predicate)(state.selection) || findParentNode(predicate)(state.selection)
if (!Object.keys(attrs).length || !node) { if (!Object.keys(attributes).length || !node) {
return !!node return !!node
} }
return node.node.hasMarkup(type, { ...node.node.attrs, ...attrs }) return node.node.hasMarkup(type, {
...node.node.attrs,
...attributes,
})
} }

View File

@@ -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 { export default function objectIncludes(object1: { [key: string ]: any }, object2: { [key: string ]: any }): boolean {
return !!Object return !!Object
.keys(object2) .keys(object2)