move plugins to extensions

This commit is contained in:
Philipp Kühn 2020-11-02 22:43:54 +01:00
parent 727442c3a5
commit 971e8838a0
8 changed files with 80 additions and 54 deletions

View File

@ -15,7 +15,6 @@ import CommandManager from './CommandManager'
import ExtensionManager from './ExtensionManager'
import EventEmitter from './EventEmitter'
import { Extensions, UnionToIntersection, PickValue } from './types'
import defaultPlugins from './plugins'
import * as extensions from './extensions'
import style from './style'
@ -264,10 +263,7 @@ export class Editor extends EventEmitter {
this.view = new EditorView(this.options.element, {
state: EditorState.create({
doc: this.createDocument(this.options.content),
plugins: [
...this.extensionManager.plugins,
...defaultPlugins.map(plugin => plugin(this.proxy)),
],
plugins: this.extensionManager.plugins,
}),
dispatchTransaction: this.dispatchTransaction.bind(this),
})

View File

@ -0,0 +1,15 @@
import { Plugin, PluginKey } from 'prosemirror-state'
import { createExtension } from '../Extension'
export const Editable = createExtension({
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('editable'),
props: {
editable: () => this.editor.options.editable,
},
}),
]
},
})

View File

@ -0,0 +1,42 @@
import { Plugin } from 'prosemirror-state'
import { createExtension } from '../Extension'
export const FocusEvents = createExtension({
addProseMirrorPlugins() {
const { editor } = this
return [
new Plugin({
props: {
attributes: {
tabindex: '0',
},
handleDOMEvents: {
focus: () => {
editor.isFocused = true
const transaction = editor.state.tr.setMeta('focused', true)
editor.view.dispatch(transaction)
return true
},
blur: () => {
editor.isFocused = false
const transaction = editor.state.tr.setMeta('focused', false)
editor.view.dispatch(transaction)
return true
},
},
},
}),
]
},
})
declare module '../Editor' {
interface AllExtensions {
FocusEvents: typeof FocusEvents,
}
}

View File

@ -2,9 +2,12 @@ export { Blur } from './blur'
export { ClearContent } from './clearContent'
export { ClearNodes } from './clearNodes'
export { DeleteSelection } from './deleteSelection'
export { Editable } from './editable'
export { Focus } from './focus'
export { FocusEvents } from './focusEvents'
export { InsertHTML } from './insertHTML'
export { InsertText } from './insertText'
export { Keymap } from './keymap'
export { LiftListItem } from './liftListItem'
export { RemoveMark } from './removeMark'
export { RemoveMarks } from './removeMarks'

View File

@ -0,0 +1,19 @@
import { keymap } from 'prosemirror-keymap'
import { baseKeymap } from 'prosemirror-commands'
import { undoInputRule } from 'prosemirror-inputrules'
import { createExtension } from '../Extension'
export const Keymap = createExtension({
addProseMirrorPlugins() {
return [
keymap({ Backspace: undoInputRule }),
keymap(baseKeymap),
]
},
})
declare module '../Editor' {
interface AllExtensions {
Keymap: typeof Keymap,
}
}

View File

@ -1,9 +0,0 @@
import { Plugin, PluginKey } from 'prosemirror-state'
import Editor from '../..'
export default (editor: Editor) => new Plugin({
key: new PluginKey('editable'),
props: {
editable: () => editor.options.editable,
},
})

View File

@ -1,28 +0,0 @@
import { Plugin } from 'prosemirror-state'
import Editor from '../..'
export default (editor: Editor) => new Plugin({
props: {
attributes: {
tabindex: '0',
},
handleDOMEvents: {
focus: () => {
editor.isFocused = true
const transaction = editor.state.tr.setMeta('focused', true)
editor.view.dispatch(transaction)
return true
},
blur: () => {
editor.isFocused = false
const transaction = editor.state.tr.setMeta('focused', false)
editor.view.dispatch(transaction)
return true
},
},
},
})

View File

@ -1,12 +0,0 @@
import { keymap } from 'prosemirror-keymap'
import { baseKeymap } from 'prosemirror-commands'
import { undoInputRule } from 'prosemirror-inputrules'
import editable from './editable'
import focus from './focus'
export default [
() => keymap({ Backspace: undoInputRule }),
() => keymap(baseKeymap),
editable,
focus,
]