feat: add editor.getAttributes, deprecate editor.getNodeAttributes and editor.getMarkAttributes

This commit is contained in:
Philipp Kühn
2021-05-07 11:10:18 +02:00
parent 58f49b563c
commit 072905cb95
5 changed files with 47 additions and 5 deletions

View File

@@ -1,8 +1,12 @@
import {
EditorState, Plugin, PluginKey, Transaction,
EditorState,
Plugin,
PluginKey,
Transaction,
} from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import { Schema } from 'prosemirror-model'
import { Schema, MarkType, NodeType } from 'prosemirror-model'
import getAttributes from './helpers/getAttributes'
import getNodeAttributes from './helpers/getNodeAttributes'
import getMarkAttributes from './helpers/getMarkAttributes'
import isActive from './helpers/isActive'
@@ -331,12 +335,21 @@ export class Editor extends EventEmitter {
})
}
/**
* Get attributes of the currently selected node or mark.
*/
public getAttributes(nameOrType: string | NodeType | MarkType): Record<string, any> {
return getAttributes(this.state, nameOrType)
}
/**
* Get attributes of the currently selected node.
*
* @param name Name of the node
*/
public getNodeAttributes(name: string): Record<string, any> {
console.warn('[tiptap warn]: editor.getNodeAttributes() is deprecated. please use editor.getAttributes() instead.')
return getNodeAttributes(this.state, name)
}
@@ -346,6 +359,8 @@ export class Editor extends EventEmitter {
* @param name Name of the mark
*/
public getMarkAttributes(name: string): Record<string, any> {
console.warn('[tiptap warn]: editor.getMarkAttributes() is deprecated. please use editor.getAttributes() instead.')
return getMarkAttributes(this.state, name)
}

View File

@@ -0,0 +1,27 @@
import { MarkType, NodeType } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state'
import getSchemaTypeNameByName from './getSchemaTypeNameByName'
import getNodeAttributes from './getNodeAttributes'
import getMarkAttributes from './getMarkAttributes'
export default function getAttributes(
state: EditorState,
typeOrName: string | NodeType | MarkType,
): Record<string, any> {
const schemaType = getSchemaTypeNameByName(
typeof typeOrName === 'string'
? typeOrName
: typeOrName.name,
state.schema,
)
if (schemaType === 'node') {
return getNodeAttributes(state, typeOrName as NodeType)
}
if (schemaType === 'mark') {
return getMarkAttributes(state, typeOrName as MarkType)
}
return {}
}

View File

@@ -21,6 +21,7 @@ export { default as generateHTML } from './helpers/generateHTML'
export { default as generateJSON } from './helpers/generateJSON'
export { default as getSchema } from './helpers/getSchema'
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
export { default as getAttributes } from './helpers/getMarkAttributes'
export { default as getMarkAttributes } from './helpers/getMarkAttributes'
export { default as getNodeAttributes } from './helpers/getNodeAttributes'
export { default as getNodeType } from './helpers/getNodeType'