fix extension manager
This commit is contained in:
@@ -31,16 +31,16 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new History(),
|
History(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new Bold(),
|
Bold(),
|
||||||
new Italic(),
|
Italic(),
|
||||||
new Code(),
|
Code(),
|
||||||
new CodeBlock(),
|
CodeBlock(),
|
||||||
new Heading(),
|
Heading(),
|
||||||
new Focus({
|
Focus({
|
||||||
className: 'has-focus',
|
className: 'has-focus',
|
||||||
nested: true,
|
nested: true,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ export default {
|
|||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
content: '<p>This is a radically reduced version of tiptap for minimalisits. It has only support for a document, paragraphs and text, that’s it.</p>',
|
content: '<p>This is a radically reduced version of tiptap for minimalisits. It has only support for a document, paragraphs and text, that’s it.</p>',
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ export default {
|
|||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
content: '<p>I’m running tiptap with Vue.js. This demo is interactive, try to edit the text.</p>',
|
content: '<p>I’m running tiptap with Vue.js. This demo is interactive, try to edit the text.</p>',
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new Bold(),
|
Bold(),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new Bold(),
|
Bold(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>This isn’t bold.</p>
|
<p>This isn’t bold.</p>
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new Code(),
|
Code(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>This isn’t code.</p>
|
<p>This isn’t code.</p>
|
||||||
|
|||||||
@@ -33,10 +33,10 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new History(),
|
History(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>Edit this text and press undo to test this extension.</p>
|
<p>Edit this text and press undo to test this extension.</p>
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new Italic(),
|
Italic(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>This isn’t italic.</p>
|
<p>This isn’t italic.</p>
|
||||||
|
|||||||
@@ -55,15 +55,15 @@ export default {
|
|||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
content: '<h2>Hey there!</h2><p>This editor is based on Prosemirror, fully extendable and renderless. You can easily add custom nodes as Vue components.</p>',
|
content: '<h2>Hey there!</h2><p>This editor is based on Prosemirror, fully extendable and renderless. You can easily add custom nodes as Vue components.</p>',
|
||||||
extensions: [
|
extensions: [
|
||||||
new Document(),
|
Document(),
|
||||||
new Paragraph(),
|
Paragraph(),
|
||||||
new Text(),
|
Text(),
|
||||||
new CodeBlock(),
|
CodeBlock(),
|
||||||
new History(),
|
History(),
|
||||||
new Bold(),
|
Bold(),
|
||||||
new Italic(),
|
Italic(),
|
||||||
new Code(),
|
Code(),
|
||||||
new Heading(),
|
Heading(),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Blockquote(),
|
Blockquote(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<blockquote>
|
<blockquote>
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new BulletList(),
|
BulletList(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<ul>
|
<ul>
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Heading({
|
Heading({
|
||||||
levels: [1, 2],
|
levels: [1, 2],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new HorizontalRule(),
|
HorizontalRule(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p>Some text.</p>
|
<p>Some text.</p>
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new OrderedList(),
|
OrderedList(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<ol>
|
<ol>
|
||||||
|
|||||||
@@ -66,10 +66,10 @@ You have to include all table extensions (`TableHeader`, `TableCell` & `TableRow
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Table(),
|
Table(),
|
||||||
new TableCell(),
|
TableCell(),
|
||||||
new TableHeader(),
|
TableHeader(),
|
||||||
new TableRow(),
|
TableRow(),
|
||||||
],
|
],
|
||||||
content: ''
|
content: ''
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Strike(),
|
Strike(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p><s>That's strikethrough.</s></p>
|
<p><s>That's strikethrough.</s></p>
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new TodoItem({
|
TodoItem({
|
||||||
nested: true,
|
nested: true,
|
||||||
}),
|
}),
|
||||||
new TodoList(),
|
TodoList(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<ul data-type="todo_list">
|
<ul data-type="todo_list">
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
editor: new Editor({
|
editor: new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
new Underline(),
|
Underline(),
|
||||||
],
|
],
|
||||||
content: `
|
content: `
|
||||||
<p><u>This is underlined.</u></p>
|
<p><u>This is underlined.</u></p>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## New features
|
## New features
|
||||||
|
|
||||||
* generate schema without initializing tiptap, to make SSR easier (e. g. `getSchema([new Doc(), new Paragraph()])`)
|
* generate schema without initializing tiptap, to make SSR easier (e. g. `getSchema([Doc(), Paragraph()])`)
|
||||||
|
|
||||||
## Requested features
|
## Requested features
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ import removeElement from './utils/removeElement'
|
|||||||
import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
||||||
import ExtensionManager from './ExtensionManager'
|
import ExtensionManager from './ExtensionManager'
|
||||||
import EventEmitter from './EventEmitter'
|
import EventEmitter from './EventEmitter'
|
||||||
|
import Extension from './Extension'
|
||||||
|
import Node from './Node'
|
||||||
|
import Mark from './Mark'
|
||||||
import ComponentRenderer from './ComponentRenderer'
|
import ComponentRenderer from './ComponentRenderer'
|
||||||
import defaultPlugins from './plugins'
|
import defaultPlugins from './plugins'
|
||||||
import * as commands from './commands'
|
import * as commands from './commands'
|
||||||
@@ -27,7 +30,7 @@ type EditorContent = string | JSON | null
|
|||||||
interface EditorOptions {
|
interface EditorOptions {
|
||||||
element: Element,
|
element: Element,
|
||||||
content: EditorContent,
|
content: EditorContent,
|
||||||
extensions: Function[],
|
extensions: (Extension | Node | Mark)[],
|
||||||
injectCSS: boolean,
|
injectCSS: boolean,
|
||||||
autoFocus: 'start' | 'end' | number | boolean | null,
|
autoFocus: 'start' | 'end' | number | boolean | null,
|
||||||
editable: boolean,
|
editable: boolean,
|
||||||
|
|||||||
@@ -78,9 +78,15 @@ export interface ExtensionExtends<Callback = ExtensionCallback> {
|
|||||||
plugins: (params: Callback) => Plugin[]
|
plugins: (params: Callback) => Plugin[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Extension<Options, Extends extends ExtensionExtends = ExtensionExtends> {
|
export default class Extension<Options = {}, Extends extends ExtensionExtends = ExtensionExtends> {
|
||||||
type = 'extension'
|
type = 'extension'
|
||||||
configs: any = {}
|
config: any = {}
|
||||||
|
configs: {
|
||||||
|
[key: string]: {
|
||||||
|
stategy: 'extend' | 'overwrite'
|
||||||
|
value: any
|
||||||
|
}[]
|
||||||
|
} = {}
|
||||||
usedOptions: Partial<Options> = {}
|
usedOptions: Partial<Options> = {}
|
||||||
|
|
||||||
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
|
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import deepmerge from 'deepmerge'
|
||||||
import collect from 'collect.js'
|
import collect from 'collect.js'
|
||||||
import { Plugin } from 'prosemirror-state'
|
import { Plugin } from 'prosemirror-state'
|
||||||
import { keymap } from 'prosemirror-keymap'
|
import { keymap } from 'prosemirror-keymap'
|
||||||
@@ -10,8 +11,7 @@ import Node from './Node'
|
|||||||
import Mark from './Mark'
|
import Mark from './Mark'
|
||||||
import capitalize from './utils/capitalize'
|
import capitalize from './utils/capitalize'
|
||||||
|
|
||||||
// type Extensions = (Extension | Node | Mark)[]
|
type Extensions = (Extension | Node | Mark)[]
|
||||||
type Extensions = Function[]
|
|
||||||
|
|
||||||
export default class ExtensionManager {
|
export default class ExtensionManager {
|
||||||
|
|
||||||
@@ -22,77 +22,106 @@ export default class ExtensionManager {
|
|||||||
this.editor = editor
|
this.editor = editor
|
||||||
this.extensions = extensions
|
this.extensions = extensions
|
||||||
this.extensions.forEach(extension => {
|
this.extensions.forEach(extension => {
|
||||||
console.log({extension})
|
const simpleConfigs = ['name', 'defaults']
|
||||||
// extension.bindEditor(editor)
|
|
||||||
// editor.on('schemaCreated', () => {
|
Object
|
||||||
// this.editor.registerCommands(extension.commands())
|
.entries(extension.configs)
|
||||||
// extension.created()
|
.sort(([name]) => simpleConfigs.includes(name) ? -1 : 1)
|
||||||
// })
|
.forEach(([name, configs]) => {
|
||||||
|
extension.config[name] = configs.reduce((accumulator, { stategy, value: rawValue }) => {
|
||||||
|
const isSimpleConfig = simpleConfigs.includes(name)
|
||||||
|
const props = isSimpleConfig
|
||||||
|
? undefined
|
||||||
|
: {
|
||||||
|
editor,
|
||||||
|
options: deepmerge(extension.config.defaults, extension.usedOptions),
|
||||||
|
type: {},
|
||||||
|
name: '',
|
||||||
|
}
|
||||||
|
const value = typeof rawValue === 'function'
|
||||||
|
? rawValue(props)
|
||||||
|
: rawValue
|
||||||
|
|
||||||
|
if (accumulator === undefined) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stategy === 'overwrite') {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stategy === 'extend') {
|
||||||
|
return deepmerge(accumulator, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}, undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
editor.on('schemaCreated', () => {
|
||||||
|
if (extension.config.commands) {
|
||||||
|
this.editor.registerCommands(extension.config.commands)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
get topNode() {
|
get topNode() {
|
||||||
const topNode = collect(this.extensions).firstWhere('topNode', true)
|
const topNode = collect(this.extensions).firstWhere('config.topNode', true)
|
||||||
|
|
||||||
if (topNode) {
|
if (topNode) {
|
||||||
return topNode.name
|
return topNode.config.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get nodes(): any {
|
get nodes(): any {
|
||||||
// return collect(this.extensions)
|
return collect(this.extensions)
|
||||||
// .where('extensionType', 'node')
|
.where('type', 'node')
|
||||||
// .mapWithKeys((extension: Node) => [extension.name, extension.schema()])
|
.mapWithKeys((extension: Node) => [extension.config.name, extension.config.schema])
|
||||||
// .all()
|
.all()
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get marks(): any {
|
get marks(): any {
|
||||||
// return collect(this.extensions)
|
return collect(this.extensions)
|
||||||
// .where('extensionType', 'mark')
|
.where('type', 'mark')
|
||||||
// .mapWithKeys((extension: Mark) => [extension.name, extension.schema()])
|
.mapWithKeys((extension: Mark) => [extension.config.name, extension.config.schema])
|
||||||
// .all()
|
.all()
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get plugins(): Plugin[] {
|
get plugins(): Plugin[] {
|
||||||
// const plugins = collect(this.extensions)
|
const plugins = collect(this.extensions)
|
||||||
// .flatMap(extension => extension.plugins())
|
.flatMap(extension => extension.config.plugins)
|
||||||
// .toArray()
|
.filter(plugin => plugin)
|
||||||
|
.toArray()
|
||||||
|
|
||||||
// return [
|
return [
|
||||||
// ...plugins,
|
...plugins,
|
||||||
// ...this.keymaps,
|
...this.keymaps,
|
||||||
// ...this.pasteRules,
|
...this.pasteRules,
|
||||||
// inputRules({ rules: this.inputRules }),
|
inputRules({ rules: this.inputRules }),
|
||||||
// ]
|
]
|
||||||
|
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get inputRules(): any {
|
get inputRules(): any {
|
||||||
// return collect(this.extensions)
|
return collect(this.extensions)
|
||||||
// .flatMap(extension => extension.inputRules())
|
.flatMap(extension => extension.config.inputRules)
|
||||||
// .toArray()
|
.filter(plugin => plugin)
|
||||||
|
.toArray()
|
||||||
return {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get pasteRules(): any {
|
get pasteRules(): any {
|
||||||
// return collect(this.extensions)
|
return collect(this.extensions)
|
||||||
// .flatMap(extension => extension.pasteRules())
|
.flatMap(extension => extension.config.pasteRules)
|
||||||
// .toArray()
|
.filter(plugin => plugin)
|
||||||
return {}
|
.toArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
get keymaps() {
|
get keymaps() {
|
||||||
// return collect(this.extensions)
|
return collect(this.extensions)
|
||||||
// .map(extension => extension.keys())
|
.map(extension => extension.config.keys)
|
||||||
// .filter(keys => !!Object.keys(keys).length)
|
.filter(keys => keys)
|
||||||
// // @ts-ignore
|
.map(keys => keymap(keys))
|
||||||
// .map(keys => keymap(keys))
|
.toArray()
|
||||||
// .toArray()
|
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get nodeViews() {
|
get nodeViews() {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user