refactoring

This commit is contained in:
Philipp Kühn
2020-10-22 11:14:44 +02:00
parent 9697d585fe
commit 930eb63564
7 changed files with 28 additions and 26 deletions

View File

@@ -119,16 +119,16 @@ import { GlobalAttributes } from './types'
export interface ExtensionSpec<Options = {}, Commands = {}> { export interface ExtensionSpec<Options = {}, Commands = {}> {
name: string, name: string,
defaultOptions?: Options, defaultOptions?: Options,
createGlobalAttributes?: ( addGlobalAttributes?: (
this: { this: {
options: Options, options: Options,
}, },
) => GlobalAttributes, ) => GlobalAttributes,
createCommands?: (this: { addCommands?: (this: {
options: Options, options: Options,
editor: Editor, editor: Editor,
}) => Commands, }) => Commands,
createShortcuts?: (this: { addKeyboardShortcuts?: (this: {
options: Options, options: Options,
editor: Editor, editor: Editor,
}) => { }) => {
@@ -147,8 +147,9 @@ export const defaultExtension: Extension = {
type: 'extension', type: 'extension',
name: 'extension', name: 'extension',
options: {}, options: {},
createGlobalAttributes: () => [], addGlobalAttributes: () => [],
createCommands: () => ({}), addCommands: () => ({}),
addKeyboardShortcuts: () => ({}),
} }
export function createExtension<Options extends {}, Commands extends {}>(config: ExtensionSpec<Options, Commands>) { export function createExtension<Options extends {}, Commands extends {}>(config: ExtensionSpec<Options, Commands>) {

View File

@@ -89,12 +89,14 @@ export default class ExtensionManager {
} }
get keymaps() { get keymaps() {
return [] return this.extensions.map(extension => {
// return collect(this.extensions) const context = {
// .map(extension => extension.config.keys) options: extension.options,
// .filter(keys => keys) editor: this.editor,
// .map(keys => keymap(keys)) }
// .toArray()
return keymap(extension.addKeyboardShortcuts.bind(context)())
})
} }
get nodeViews() { get nodeViews() {

View File

@@ -21,7 +21,7 @@ export interface MarkExtensionSpec<Options = {}, Commands = {}> extends Extensio
attributes: { [key: string]: any }, attributes: { [key: string]: any },
} }
) => DOMOutputSpec, ) => DOMOutputSpec,
createAttributes?: ( addAttributes?: (
this: { this: {
options: Options, options: Options,
}, },
@@ -45,7 +45,7 @@ const defaultMark: MarkExtension = {
spanning: null, spanning: null,
parseHTML: () => null, parseHTML: () => null,
renderHTML: () => null, renderHTML: () => null,
createAttributes: () => ({}), addAttributes: () => ({}),
} }
export function createMark<Options extends {}, Commands extends {}>(config: MarkExtensionSpec<Options, Commands>) { export function createMark<Options extends {}, Commands extends {}>(config: MarkExtensionSpec<Options, Commands>) {

View File

@@ -4,6 +4,10 @@ import { Attributes } from './types'
export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> { export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
topNode?: boolean, topNode?: boolean,
/**
* content
*/
content?: NodeSpec['content'], content?: NodeSpec['content'],
marks?: NodeSpec['marks'], marks?: NodeSpec['marks'],
group?: NodeSpec['group'], group?: NodeSpec['group'],
@@ -28,7 +32,7 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
attributes: { [key: string]: any }, attributes: { [key: string]: any },
} }
) => DOMOutputSpec, ) => DOMOutputSpec,
createAttributes?: ( addAttributes?: (
this: { this: {
options: Options, options: Options,
}, },
@@ -59,7 +63,7 @@ const defaultNode: NodeExtension = {
isolating: null, isolating: null,
parseHTML: () => null, parseHTML: () => null,
renderHTML: () => null, renderHTML: () => null,
createAttributes: () => ({}), addAttributes: () => ({}),
} }
export function createNode<Options extends {}, Commands extends {}>(config: NodeExtensionSpec<Options, Commands>) { export function createNode<Options extends {}, Commands extends {}>(config: NodeExtensionSpec<Options, Commands>) {

View File

@@ -23,7 +23,7 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
options: extension.options, options: extension.options,
} }
const globalAttributes = extension.createGlobalAttributes.bind(context)() as GlobalAttributes const globalAttributes = extension.addGlobalAttributes.bind(context)() as GlobalAttributes
globalAttributes.forEach(globalAttribute => { globalAttributes.forEach(globalAttribute => {
globalAttribute.types.forEach(type => { globalAttribute.types.forEach(type => {
@@ -48,7 +48,7 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
options: extension.options, options: extension.options,
} }
const attributes = extension.createAttributes.bind(context)() as Attributes const attributes = extension.addAttributes.bind(context)() as Attributes
Object Object
.entries(attributes) .entries(attributes)

View File

@@ -1,17 +1,12 @@
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model' import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
import { Extensions } from '../types' import { Extensions } from '../types'
// import getTopNodeFromExtensions from './getTopNodeFromExtensions'
// import getNodesFromExtensions from './getNodesFromExtensions'
// import getMarksFromExtensions from './getMarksFromExtensions'
import splitExtensions from './splitExtensions' import splitExtensions from './splitExtensions'
import getAttributesFromExtensions from './getAttributesFromExtensions' import getAttributesFromExtensions from './getAttributesFromExtensions'
import getRenderedAttributes from './getRenderedAttributes' import getRenderedAttributes from './getRenderedAttributes'
export default function getSchema(extensions: Extensions): Schema { export default function getSchema(extensions: Extensions): Schema {
const allAttributes = getAttributesFromExtensions(extensions) const allAttributes = getAttributesFromExtensions(extensions)
const { nodeExtensions, markExtensions } = splitExtensions(extensions) const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const topNode = nodeExtensions.find(extension => extension.topNode)?.name const topNode = nodeExtensions.find(extension => extension.topNode)?.name
const nodes = Object.fromEntries(nodeExtensions.map(extension => { const nodes = Object.fromEntries(nodeExtensions.map(extension => {

View File

@@ -16,7 +16,7 @@ export default createNode({
content: 'inline*', content: 'inline*',
createGlobalAttributes() { addGlobalAttributes() {
return [ return [
{ {
types: ['paragraph'], types: ['paragraph'],
@@ -33,7 +33,7 @@ export default createNode({
] ]
}, },
createAttributes() { addAttributes() {
return { return {
id: { id: {
default: '123', default: '123',
@@ -56,7 +56,7 @@ export default createNode({
return ['p', attributes, 0] return ['p', attributes, 0]
}, },
createCommands() { addCommands() {
return { return {
paragraph: () => ({ commands }) => { paragraph: () => ({ commands }) => {
return commands.toggleBlockType('paragraph', 'paragraph') return commands.toggleBlockType('paragraph', 'paragraph')
@@ -64,7 +64,7 @@ export default createNode({
} }
}, },
createShortcuts() { addKeyboardShortcuts() {
return { return {
'Mod-Alt-0': () => this.editor.paragraph(), 'Mod-Alt-0': () => this.editor.paragraph(),
} }