refactoring

This commit is contained in:
Philipp Kühn
2021-09-22 22:45:27 +02:00
parent f4e3b07672
commit e5c765c8e4
9 changed files with 30 additions and 22 deletions

View File

@@ -11,9 +11,6 @@ export const FocusEvents = Extension.create({
new Plugin({ new Plugin({
key: new PluginKey('focusEvents'), key: new PluginKey('focusEvents'),
props: { props: {
attributes: {
tabindex: '0',
},
handleDOMEvents: { handleDOMEvents: {
focus: (view, event) => { focus: (view, event) => {
editor.isFocused = true editor.isFocused = true

View File

@@ -3,3 +3,4 @@ export { Commands } from './commands'
export { Editable } from './editable' export { Editable } from './editable'
export { FocusEvents } from './focusEvents' export { FocusEvents } from './focusEvents'
export { Keymap } from './keymap' export { Keymap } from './keymap'
export { Tabindex } from './tabindex'

View File

@@ -0,0 +1,19 @@
import { Plugin, PluginKey } from 'prosemirror-state'
import { Extension } from '../Extension'
export const Tabindex = Extension.create({
name: 'tabindex',
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('tabindex'),
props: {
attributes: {
tabindex: '0',
},
},
}),
]
},
})

View File

@@ -17,9 +17,9 @@ export default function getMarkAttributes(state: EditorState, typeOrName: string
const mark = marks.find(markItem => markItem.type.name === type.name) const mark = marks.find(markItem => markItem.type.name === type.name)
if (mark) { if (!mark) {
return { ...mark.attrs } return {}
} }
return {} return { ...mark.attrs }
} }

View File

@@ -15,9 +15,9 @@ export default function getNodeAttributes(state: EditorState, typeOrName: string
.reverse() .reverse()
.find(nodeItem => nodeItem.type.name === type.name) .find(nodeItem => nodeItem.type.name === type.name)
if (node) { if (!node) {
return { ...node.attrs } return {}
} }
return {} return { ...node.attrs }
} }

View File

@@ -14,7 +14,5 @@ export default function getRenderedAttributes(nodeOrMark: Node | Mark, extension
return item.attribute.renderHTML(nodeOrMark.attrs) || {} return item.attribute.renderHTML(nodeOrMark.attrs) || {}
}) })
.reduce((attributes, attribute) => { .reduce((attributes, attribute) => mergeAttributes(attributes, attribute), {})
return mergeAttributes(attributes, attribute)
}, {})
} }

View File

@@ -1,13 +1,5 @@
import { MarkType, NodeType, Schema } from 'prosemirror-model' import { MarkType, NodeType, Schema } from 'prosemirror-model'
export default function getSchemaTypeByName(name: string, schema: Schema): NodeType | MarkType | null { export default function getSchemaTypeByName(name: string, schema: Schema): NodeType | MarkType | null {
if (schema.nodes[name]) { return schema.nodes[name] || schema.marks[name] || null
return schema.nodes[name]
}
if (schema.marks[name]) {
return schema.marks[name]
}
return null
} }

View File

@@ -9,6 +9,7 @@ export default function mergeAttributes(...objects: Record<string, any>[]): Reco
if (!exists) { if (!exists) {
mergedAttributes[key] = value mergedAttributes[key] = value
return return
} }

View File

@@ -1,5 +1,5 @@
export default function removeElement(element: HTMLElement): void { export default function removeElement(element: HTMLElement): void {
if (element && element.parentNode) { if (element?.parentNode) {
element.parentNode.removeChild(element) element.parentNode.removeChild(element)
} }
} }