replace extensions
This commit is contained in:
@@ -1,54 +1,151 @@
|
|||||||
|
import cloneDeep from 'clone-deep'
|
||||||
import { Plugin } from 'prosemirror-state'
|
import { Plugin } from 'prosemirror-state'
|
||||||
import { Editor, Command } from './Editor'
|
import { Editor, CommandSpec } from './Editor'
|
||||||
|
|
||||||
export default abstract class Extension {
|
// export default abstract class Extension {
|
||||||
|
|
||||||
constructor(options = {}) {
|
// constructor(options = {}) {
|
||||||
this.options = {
|
// this.options = {
|
||||||
...this.defaultOptions(),
|
// ...this.defaultOptions(),
|
||||||
...options,
|
// ...options,
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// editor!: Editor
|
||||||
|
// options: { [key: string]: any } = {}
|
||||||
|
|
||||||
|
// public abstract name: string
|
||||||
|
|
||||||
|
// public extensionType = 'extension'
|
||||||
|
|
||||||
|
// public created() {}
|
||||||
|
|
||||||
|
// public bindEditor(editor: Editor): void {
|
||||||
|
// this.editor = editor
|
||||||
|
// }
|
||||||
|
|
||||||
|
// defaultOptions(): { [key: string]: any } {
|
||||||
|
// return {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// update(): any {
|
||||||
|
// return () => {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// plugins(): Plugin[] {
|
||||||
|
// return []
|
||||||
|
// }
|
||||||
|
|
||||||
|
// inputRules(): any {
|
||||||
|
// return []
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pasteRules(): any {
|
||||||
|
// return []
|
||||||
|
// }
|
||||||
|
|
||||||
|
// keys(): { [key: string]: Function } {
|
||||||
|
// return {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// commands(): { [key: string]: Command } {
|
||||||
|
// return {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
type AnyObject = {
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
|
||||||
|
type NoInfer<T> = [T][T extends any ? 0 : never]
|
||||||
|
|
||||||
|
export interface ExtensionCallback {
|
||||||
|
name: string
|
||||||
|
editor: Editor
|
||||||
|
options: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExtensionExtends<Callback = ExtensionCallback> {
|
||||||
|
name: string
|
||||||
|
options: AnyObject
|
||||||
|
commands: (params: Callback) => CommandSpec
|
||||||
|
inputRules: (params: Callback) => any[]
|
||||||
|
pasteRules: (params: Callback) => any[]
|
||||||
|
keys: (params: Callback) => {
|
||||||
|
[key: string]: Function
|
||||||
|
}
|
||||||
|
plugins: (params: Callback) => Plugin[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class Extension<Options, Extends extends ExtensionExtends = ExtensionExtends> {
|
||||||
|
type = 'extension'
|
||||||
|
configs: any = {}
|
||||||
|
usedOptions: Partial<Options> = {}
|
||||||
|
|
||||||
|
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
|
||||||
|
const item = {
|
||||||
|
stategy,
|
||||||
|
value,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.configs[key]) {
|
||||||
|
this.configs[key].push(item)
|
||||||
|
} else {
|
||||||
|
this.configs[key] = [item]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
editor!: Editor
|
public configure(options: Partial<Options>) {
|
||||||
options: { [key: string]: any } = {}
|
this.usedOptions = { ...this.usedOptions, ...options }
|
||||||
|
return this
|
||||||
public abstract name: string
|
|
||||||
|
|
||||||
public extensionType = 'extension'
|
|
||||||
|
|
||||||
public created() {}
|
|
||||||
|
|
||||||
public bindEditor(editor: Editor): void {
|
|
||||||
this.editor = editor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultOptions(): { [key: string]: any } {
|
public name(value: Extends['name']) {
|
||||||
return {}
|
this.storeConfig('name', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
update(): any {
|
public defaults(value: Options) {
|
||||||
return () => {}
|
this.storeConfig('defaults', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins(): Plugin[] {
|
public commands(value: Extends['commands']) {
|
||||||
return []
|
this.storeConfig('commands', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
inputRules(): any {
|
public keys(value: Extends['keys']) {
|
||||||
return []
|
this.storeConfig('keys', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
pasteRules(): any {
|
public inputRules(value: Extends['inputRules']) {
|
||||||
return []
|
this.storeConfig('inputRules', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
keys(): { [key: string]: Function } {
|
public pasteRules(value: Extends['pasteRules']) {
|
||||||
return {}
|
this.storeConfig('pasteRules', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
commands(): { [key: string]: Command } {
|
public plugins(value: Extends['plugins']) {
|
||||||
return {}
|
this.storeConfig('plugins', value, 'overwrite')
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public extend<T extends Extract<keyof Extends, string>>(key: T, value: Extends[T]) {
|
||||||
|
this.storeConfig(key, value, 'extend')
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
public create() {
|
||||||
|
type ParentOptions = Options
|
||||||
|
|
||||||
|
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
|
||||||
|
return cloneDeep(this, true).configure(options as Options)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,37 @@
|
|||||||
import Extension from './Extension'
|
import { MarkSpec, MarkType } from 'prosemirror-model'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
import Extension, { ExtensionCallback, ExtensionExtends } from './Extension'
|
||||||
|
|
||||||
export default abstract class Mark extends Extension {
|
// export default abstract class Mark extends Extension {
|
||||||
|
|
||||||
constructor(options = {}) {
|
// constructor(options = {}) {
|
||||||
super(options)
|
// super(options)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public extensionType = 'mark'
|
||||||
|
|
||||||
|
// abstract schema(): MarkSpec
|
||||||
|
|
||||||
|
// get type() {
|
||||||
|
// return this.editor.schema.marks[this.name]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
export interface MarkCallback extends ExtensionCallback {
|
||||||
|
// TODO: fix optional
|
||||||
|
type?: MarkType
|
||||||
}
|
}
|
||||||
|
|
||||||
public extensionType = 'mark'
|
export interface MarkExtends<Callback = MarkCallback> extends ExtensionExtends<Callback> {
|
||||||
|
topMark: boolean
|
||||||
abstract schema(): MarkSpec
|
schema: (params: Callback) => MarkSpec
|
||||||
|
|
||||||
get type() {
|
|
||||||
return this.editor.schema.marks[this.name]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default class Mark<Options = {}> extends Extension<Options, MarkExtends> {
|
||||||
|
type = 'node'
|
||||||
|
|
||||||
|
public schema(value: MarkExtends['schema']) {
|
||||||
|
this.storeConfig('schema', value, 'overwrite')
|
||||||
|
return this
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,44 @@
|
|||||||
import Extension from './Extension'
|
import { NodeSpec, NodeType } from 'prosemirror-model'
|
||||||
import { NodeSpec } from 'prosemirror-model'
|
import Extension, { ExtensionCallback, ExtensionExtends } from './Extension'
|
||||||
|
|
||||||
export default abstract class Node extends Extension {
|
// export default abstract class Node extends Extension {
|
||||||
|
|
||||||
constructor(options = {}) {
|
// constructor(options = {}) {
|
||||||
super(options)
|
// super(options)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public extensionType = 'node'
|
||||||
|
|
||||||
|
// public topNode = false
|
||||||
|
|
||||||
|
// abstract schema(): NodeSpec
|
||||||
|
|
||||||
|
// get type() {
|
||||||
|
// return this.editor.schema.nodes[this.name]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
export interface NodeCallback extends ExtensionCallback {
|
||||||
|
// TODO: fix optional
|
||||||
|
type?: NodeType
|
||||||
}
|
}
|
||||||
|
|
||||||
public extensionType = 'node'
|
export interface NodeExtends<Callback = NodeCallback> extends ExtensionExtends<Callback> {
|
||||||
|
topNode: boolean
|
||||||
public topNode = false
|
schema: (params: Callback) => NodeSpec
|
||||||
|
|
||||||
abstract schema(): NodeSpec
|
|
||||||
|
|
||||||
get type() {
|
|
||||||
return this.editor.schema.nodes[this.name]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default class Node<Options = {}> extends Extension<Options, NodeExtends> {
|
||||||
|
type = 'node'
|
||||||
|
|
||||||
|
public topNode(value: NodeExtends['topNode'] = true) {
|
||||||
|
this.storeConfig('topNode', value, 'overwrite')
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
public schema(value: NodeExtends['schema']) {
|
||||||
|
this.storeConfig('schema', value, 'overwrite')
|
||||||
|
return this
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NodeSpec, NodeType } from "prosemirror-model";
|
import { NodeSpec, NodeType } from "prosemirror-model";
|
||||||
import deepmerge from 'deepmerge'
|
import deepmerge from 'deepmerge'
|
||||||
import collect from 'collect.js'
|
import collect from 'collect.js'
|
||||||
import { Editor, CommandSpec } from '@tiptap/core'
|
import { Editor, CommandSpec, ComponentRenderer } from '@tiptap/core'
|
||||||
import cloneDeep from 'clone-deep'
|
import cloneDeep from 'clone-deep'
|
||||||
import { Plugin } from "prosemirror-state";
|
import { Plugin } from "prosemirror-state";
|
||||||
|
|
||||||
@@ -815,7 +815,7 @@ class ExtensionTest<Options, Extends extends ExtensionExtends = ExtensionExtends
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public options(options: Partial<Options>) {
|
public configure(options: Partial<Options>) {
|
||||||
this.usedOptions = { ...this.usedOptions, ...options }
|
this.usedOptions = { ...this.usedOptions, ...options }
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -864,7 +864,7 @@ class ExtensionTest<Options, Extends extends ExtensionExtends = ExtensionExtends
|
|||||||
type ParentOptions = Options
|
type ParentOptions = Options
|
||||||
|
|
||||||
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
|
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
|
||||||
return cloneDeep(this, true).options(options as Options)
|
return cloneDeep(this, true).configure(options as Options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -922,11 +922,83 @@ const Suggestion = new NodeTest<TestOptions>()
|
|||||||
}))
|
}))
|
||||||
.create()
|
.create()
|
||||||
|
|
||||||
const Blub = new ExtensionTest<TestOptions>()
|
// TODO: Erweitern
|
||||||
.name('bla')
|
// const CustomHeadlineAligned = new Headline()
|
||||||
.create()
|
// .name('custom_headline')
|
||||||
|
// .extend('defaults', {
|
||||||
|
// levels: [1, 2, 3],
|
||||||
|
// class: 'font-xs text-outline text-center',
|
||||||
|
// alignments: ['left', 'center', 'right'],
|
||||||
|
// })
|
||||||
|
|
||||||
console.log(Suggestion(), Suggestion().topNode().options({ trigger: 'jo' }))
|
// const CustomHeadlineTag = new Headline()
|
||||||
|
// .name('custom_headline')
|
||||||
|
// .configure({
|
||||||
|
// class: 'custom-headline',
|
||||||
|
// })
|
||||||
|
// .schema(() => ({
|
||||||
|
// toDOM: () => ['h1', 0],
|
||||||
|
// toVue: Component,
|
||||||
|
// }))
|
||||||
|
// .merge('schema', () => ({
|
||||||
|
// parseDOM: [
|
||||||
|
// {
|
||||||
|
// tag: 'x-custom-headline',
|
||||||
|
// }
|
||||||
|
// ],
|
||||||
|
// }))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// const Blub = new ExtensionTest<TestOptions>()
|
||||||
|
// .name('bla')
|
||||||
|
// .create()
|
||||||
|
|
||||||
|
console.log(Suggestion())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// export const Suggestion = new Suggestion()
|
||||||
|
// .name('suggestion')
|
||||||
|
// .defaults({
|
||||||
|
// trigger: '@',
|
||||||
|
// levels: [1, 2, 3],
|
||||||
|
// })
|
||||||
|
// .extend('schema', ({ editor, name, type}) => ({
|
||||||
|
// // levels: [1, 2, 3, 4, 5, 6],
|
||||||
|
// toDOM: () => ['strong', 0],
|
||||||
|
// }))
|
||||||
|
// .create()
|
||||||
|
|
||||||
|
|
||||||
|
// const Mention = Suggestion()
|
||||||
|
// .name('mention')
|
||||||
|
// .configure({
|
||||||
|
// trigger: '@'
|
||||||
|
// })
|
||||||
|
// .create()
|
||||||
|
|
||||||
|
// const Hashtag = Suggestion()
|
||||||
|
// .name('hashtag')
|
||||||
|
// .options({
|
||||||
|
// trigger: '#'
|
||||||
|
// })
|
||||||
|
// .create()
|
||||||
|
|
||||||
|
|
||||||
|
// new Editor({
|
||||||
|
// extensions: [
|
||||||
|
// // Mention({}),
|
||||||
|
// // Hashtag(),
|
||||||
|
// // Suggestion({ trigger: '#'}).name('hashtag'),
|
||||||
|
|
||||||
|
// // Suggestion.option({ trigger: '@'}).name('mention'),
|
||||||
|
// ]
|
||||||
|
// })
|
||||||
|
|
||||||
// interface MentionOptions {
|
// interface MentionOptions {
|
||||||
// trigger: string
|
// trigger: string
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Mark, CommandSpec, markInputRule, markPasteRule } from '@tiptap/core'
|
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
|
|
||||||
declare module '@tiptap/core/src/Editor' {
|
declare module '@tiptap/core/src/Editor' {
|
||||||
@@ -8,12 +7,9 @@ declare module '@tiptap/core/src/Editor' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Bold extends Mark {
|
export default new Mark()
|
||||||
|
.name('bold')
|
||||||
name = 'bold'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): MarkSpec {
|
|
||||||
return {
|
|
||||||
parseDOM: [
|
parseDOM: [
|
||||||
{
|
{
|
||||||
tag: 'strong',
|
tag: 'strong',
|
||||||
@@ -28,55 +24,45 @@ export default class Bold extends Mark {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
toDOM: () => ['strong', 0],
|
toDOM: () => ['strong', 0],
|
||||||
}
|
}))
|
||||||
}
|
.commands(({ editor, name }) => ({
|
||||||
|
|
||||||
commands(): CommandSpec {
|
|
||||||
return {
|
|
||||||
bold: next => () => {
|
bold: next => () => {
|
||||||
this.editor.toggleMark(this.name)
|
editor.toggleMark(name)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}))
|
||||||
}
|
.keys(({ editor }) => ({
|
||||||
|
'Mod-b': () => editor.bold()
|
||||||
|
}))
|
||||||
|
// .inputRules(({ type }) => {
|
||||||
|
// return ['**', '__'].map(character => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .endOfLine()
|
||||||
|
|
||||||
keys() {
|
// return markInputRule(regex, type)
|
||||||
return {
|
// })
|
||||||
'Mod-b': () => this.editor.bold()
|
// })
|
||||||
}
|
// .pasteRules(({ type }) => {
|
||||||
}
|
// return ['**', '__'].map(character => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .endCapture()
|
||||||
|
|
||||||
inputRules() {
|
// return markPasteRule(regex, type)
|
||||||
return ['**', '__'].map(character => {
|
// })
|
||||||
const regex = VerEx()
|
// })
|
||||||
.add('(?:^|\\s)')
|
.create()
|
||||||
.beginCapture()
|
|
||||||
.find(character)
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut(character)
|
|
||||||
.endCapture()
|
|
||||||
.find(character)
|
|
||||||
.endCapture()
|
|
||||||
.endOfLine()
|
|
||||||
|
|
||||||
return markInputRule(regex, this.type)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pasteRules() {
|
|
||||||
return ['**', '__'].map(character => {
|
|
||||||
const regex = VerEx()
|
|
||||||
.add('(?:^|\\s)')
|
|
||||||
.beginCapture()
|
|
||||||
.find(character)
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut(character)
|
|
||||||
.endCapture()
|
|
||||||
.find(character)
|
|
||||||
.endCapture()
|
|
||||||
|
|
||||||
return markPasteRule(regex, this.type)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
|
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
|
|
||||||
declare module '@tiptap/core/src/Editor' {
|
declare module '@tiptap/core/src/Editor' {
|
||||||
@@ -8,62 +7,49 @@ declare module '@tiptap/core/src/Editor' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Code extends Mark {
|
export default new Mark()
|
||||||
|
.name('code')
|
||||||
name = 'code'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): MarkSpec {
|
|
||||||
return {
|
|
||||||
excludes: '_',
|
excludes: '_',
|
||||||
parseDOM: [
|
parseDOM: [
|
||||||
{ tag: 'code' },
|
{ tag: 'code' },
|
||||||
],
|
],
|
||||||
toDOM: () => ['code', 0],
|
toDOM: () => ['code', 0],
|
||||||
}
|
}))
|
||||||
}
|
.commands(({ editor, name }) => ({
|
||||||
|
|
||||||
commands(): CommandSpec {
|
|
||||||
return {
|
|
||||||
code: next => () => {
|
code: next => () => {
|
||||||
this.editor.toggleMark(this.name)
|
editor.toggleMark(name)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}))
|
||||||
}
|
.keys(({ editor }) => ({
|
||||||
|
'Mod-`': () => editor.code()
|
||||||
|
}))
|
||||||
|
// .inputRules(({ type }) => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find('`')
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut('`')
|
||||||
|
// .endCapture()
|
||||||
|
// .find('`')
|
||||||
|
// .endCapture()
|
||||||
|
// .endOfLine()
|
||||||
|
|
||||||
keys() {
|
// return [markInputRule(regex, type)]
|
||||||
return {
|
// })
|
||||||
'Mod-`': () => this.editor.code()
|
// .pasteRules(({ type }) => {
|
||||||
}
|
// const regex = VerEx()
|
||||||
}
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find('`')
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut('`')
|
||||||
|
// .endCapture()
|
||||||
|
// .find('`')
|
||||||
|
// .endCapture()
|
||||||
|
|
||||||
inputRules() {
|
// return [markPasteRule(regex, type)]
|
||||||
const regex = VerEx()
|
// })
|
||||||
.add('(?:^|\\s)')
|
.create()
|
||||||
.beginCapture()
|
|
||||||
.find('`')
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut('`')
|
|
||||||
.endCapture()
|
|
||||||
.find('`')
|
|
||||||
.endCapture()
|
|
||||||
.endOfLine()
|
|
||||||
|
|
||||||
return markInputRule(regex, this.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
pasteRules() {
|
|
||||||
const regex = VerEx()
|
|
||||||
.add('(?:^|\\s)')
|
|
||||||
.beginCapture()
|
|
||||||
.find('`')
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut('`')
|
|
||||||
.endCapture()
|
|
||||||
.find('`')
|
|
||||||
.endCapture()
|
|
||||||
|
|
||||||
return markPasteRule(regex, this.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { Node } from '@tiptap/core'
|
import { Node } from '@tiptap/core'
|
||||||
import { NodeSpec } from 'prosemirror-model'
|
|
||||||
|
|
||||||
export default class CodeBlock extends Node {
|
export default new Node()
|
||||||
|
.name('code_block')
|
||||||
name = 'code_block'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): NodeSpec {
|
|
||||||
return {
|
|
||||||
content: 'text*',
|
content: 'text*',
|
||||||
marks: '',
|
marks: '',
|
||||||
group: 'block',
|
group: 'block',
|
||||||
@@ -17,7 +13,5 @@ export default class CodeBlock extends Node {
|
|||||||
{ tag: 'pre', preserveWhitespace: 'full' },
|
{ tag: 'pre', preserveWhitespace: 'full' },
|
||||||
],
|
],
|
||||||
toDOM: () => ['pre', ['code', 0]],
|
toDOM: () => ['pre', ['code', 0]],
|
||||||
}
|
}))
|
||||||
}
|
.create()
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
import { Node } from '@tiptap/core'
|
import { Node } from '@tiptap/core'
|
||||||
import { NodeSpec } from 'prosemirror-model'
|
|
||||||
|
|
||||||
export default class Document extends Node {
|
export default new Node()
|
||||||
|
.name('document')
|
||||||
name = 'document'
|
.topNode()
|
||||||
|
.schema(() => ({
|
||||||
topNode = true
|
|
||||||
|
|
||||||
schema(): NodeSpec {
|
|
||||||
return {
|
|
||||||
content: 'block+',
|
content: 'block+',
|
||||||
}
|
}))
|
||||||
}
|
.create()
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,27 +7,17 @@ interface FocusOptions {
|
|||||||
nested: boolean,
|
nested: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Focus extends Extension {
|
export default new Extension<FocusOptions>()
|
||||||
|
.name('focus')
|
||||||
name = 'focus'
|
.defaults({
|
||||||
|
|
||||||
constructor(options: Partial<FocusOptions> = {}) {
|
|
||||||
super(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultOptions(): FocusOptions {
|
|
||||||
return {
|
|
||||||
className: 'has-focus',
|
className: 'has-focus',
|
||||||
nested: false,
|
nested: false,
|
||||||
}
|
})
|
||||||
}
|
.plugins(({ editor, options }) => [
|
||||||
|
|
||||||
plugins() {
|
|
||||||
return [
|
|
||||||
new Plugin({
|
new Plugin({
|
||||||
props: {
|
props: {
|
||||||
decorations: ({ doc, selection }) => {
|
decorations: ({ doc, selection }) => {
|
||||||
const { isEditable, isFocused } = this.editor
|
const { isEditable, isFocused } = editor
|
||||||
const { anchor } = selection
|
const { anchor } = selection
|
||||||
const decorations: Decoration[] = []
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
@@ -40,19 +30,17 @@ export default class Focus extends Extension {
|
|||||||
|
|
||||||
if (hasAnchor && !node.isText) {
|
if (hasAnchor && !node.isText) {
|
||||||
const decoration = Decoration.node(pos, pos + node.nodeSize, {
|
const decoration = Decoration.node(pos, pos + node.nodeSize, {
|
||||||
class: this.options.className,
|
class: options.className,
|
||||||
})
|
})
|
||||||
decorations.push(decoration)
|
decorations.push(decoration)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.options.nested
|
return options.nested
|
||||||
})
|
})
|
||||||
|
|
||||||
return DecorationSet.create(doc, decorations)
|
return DecorationSet.create(doc, decorations)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
]
|
])
|
||||||
}
|
.create()
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,22 +15,12 @@ declare module '@tiptap/core/src/Editor' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Heading extends Node {
|
export default new Node<HeadingOptions>()
|
||||||
|
.name('heading')
|
||||||
name = 'heading'
|
.defaults({
|
||||||
|
|
||||||
constructor(options: Partial<HeadingOptions> = {}) {
|
|
||||||
super(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultOptions(): HeadingOptions {
|
|
||||||
return {
|
|
||||||
levels: [1, 2, 3, 4, 5, 6],
|
levels: [1, 2, 3, 4, 5, 6],
|
||||||
}
|
})
|
||||||
}
|
.schema(({ options }) => ({
|
||||||
|
|
||||||
schema(): NodeSpec {
|
|
||||||
return {
|
|
||||||
attrs: {
|
attrs: {
|
||||||
level: {
|
level: {
|
||||||
default: 1,
|
default: 1,
|
||||||
@@ -40,35 +30,29 @@ export default class Heading extends Node {
|
|||||||
group: 'block',
|
group: 'block',
|
||||||
defining: true,
|
defining: true,
|
||||||
draggable: false,
|
draggable: false,
|
||||||
parseDOM: this.options.levels
|
parseDOM: options.levels
|
||||||
.map((level: Level) => ({
|
.map((level: Level) => ({
|
||||||
tag: `h${level}`,
|
tag: `h${level}`,
|
||||||
attrs: { level },
|
attrs: { level },
|
||||||
})),
|
})),
|
||||||
toDOM: node => [`h${node.attrs.level}`, 0],
|
toDOM: node => [`h${node.attrs.level}`, 0],
|
||||||
}
|
}))
|
||||||
}
|
.commands(({ editor, name }) => ({
|
||||||
|
[name]: next => attrs => {
|
||||||
commands(): CommandSpec {
|
editor.toggleNode(name, 'paragraph', attrs)
|
||||||
return {
|
|
||||||
heading: next => attrs => {
|
|
||||||
this.editor.toggleNode(this.name, 'paragraph', attrs)
|
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}))
|
||||||
}
|
// .inputRules(({ options, type }) => {
|
||||||
|
// return options.levels.map((level: Level) => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .startOfLine()
|
||||||
|
// .find('#')
|
||||||
|
// .repeatPrevious(level)
|
||||||
|
// .whitespace()
|
||||||
|
// .endOfLine()
|
||||||
|
|
||||||
inputRules() {
|
// return textblockTypeInputRule(regex, type, { level })
|
||||||
return this.options.levels.map((level: Level) => {
|
// })
|
||||||
const regex = VerEx()
|
// })
|
||||||
.startOfLine()
|
.create()
|
||||||
.find('#')
|
|
||||||
.repeatPrevious(level)
|
|
||||||
.whitespace()
|
|
||||||
.endOfLine()
|
|
||||||
|
|
||||||
return textblockTypeInputRule(regex, this.type, { level })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Extension, CommandSpec } from '@tiptap/core'
|
import { Extension } from '@tiptap/core'
|
||||||
import {
|
import {
|
||||||
history,
|
history,
|
||||||
undo,
|
undo,
|
||||||
@@ -15,25 +15,15 @@ declare module '@tiptap/core/src/Editor' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface HistoryOptions {
|
interface HistoryOptions {
|
||||||
historyPluginOptions?: Object,
|
historyPluginOptions: Object,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class History extends Extension {
|
export default new Extension<HistoryOptions>()
|
||||||
|
.name('history')
|
||||||
name = 'history'
|
.defaults({
|
||||||
|
|
||||||
constructor(options: Partial<HistoryOptions> = {}) {
|
|
||||||
super(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultOptions(): HistoryOptions {
|
|
||||||
return {
|
|
||||||
historyPluginOptions: {},
|
historyPluginOptions: {},
|
||||||
}
|
})
|
||||||
}
|
.commands(() => ({
|
||||||
|
|
||||||
commands(): CommandSpec {
|
|
||||||
return {
|
|
||||||
undo: (next, { view }) => () => {
|
undo: (next, { view }) => () => {
|
||||||
undo(view.state, view.dispatch)
|
undo(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
@@ -42,21 +32,13 @@ export default class History extends Extension {
|
|||||||
redo(view.state, view.dispatch)
|
redo(view.state, view.dispatch)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}))
|
||||||
}
|
.keys(({ editor }) => ({
|
||||||
|
'Mod-z': () => editor.undo(),
|
||||||
keys() {
|
'Mod-y': () => editor.redo(),
|
||||||
return {
|
'Shift-Mod-z': () => editor.redo(),
|
||||||
'Mod-z': () => this.editor.undo(),
|
}))
|
||||||
'Mod-y': () => this.editor.redo(),
|
.plugins(({ options }) => [
|
||||||
'Shift-Mod-z': () => this.editor.redo(),
|
history(options.historyPluginOptions)
|
||||||
}
|
])
|
||||||
}
|
.create()
|
||||||
|
|
||||||
plugins() {
|
|
||||||
return [
|
|
||||||
history(this.options.historyPluginOptions)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
|
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
|
||||||
import { MarkSpec } from 'prosemirror-model'
|
|
||||||
import VerEx from 'verbal-expressions'
|
import VerEx from 'verbal-expressions'
|
||||||
|
|
||||||
declare module '@tiptap/core/src/Editor' {
|
declare module '@tiptap/core/src/Editor' {
|
||||||
@@ -8,67 +7,54 @@ declare module '@tiptap/core/src/Editor' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Italic extends Mark {
|
export default new Mark()
|
||||||
|
.name('italic')
|
||||||
name = 'italic'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): MarkSpec {
|
|
||||||
return {
|
|
||||||
parseDOM: [
|
parseDOM: [
|
||||||
{ tag: 'i' },
|
{ tag: 'i' },
|
||||||
{ tag: 'em' },
|
{ tag: 'em' },
|
||||||
{ style: 'font-style=italic' },
|
{ style: 'font-style=italic' },
|
||||||
],
|
],
|
||||||
toDOM: () => ['em', 0],
|
toDOM: () => ['em', 0],
|
||||||
}
|
}))
|
||||||
}
|
.commands(({ editor, name }) => ({
|
||||||
|
|
||||||
commands(): CommandSpec {
|
|
||||||
return {
|
|
||||||
italic: next => () => {
|
italic: next => () => {
|
||||||
this.editor.toggleMark(this.name)
|
editor.toggleMark(name)
|
||||||
next()
|
next()
|
||||||
},
|
},
|
||||||
}
|
}))
|
||||||
}
|
.keys(({ editor }) => ({
|
||||||
|
'Mod-i': () => editor.italic()
|
||||||
|
}))
|
||||||
|
// .inputRules(({ type }) => {
|
||||||
|
// return ['*', '_'].map(character => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .endOfLine()
|
||||||
|
|
||||||
keys() {
|
// return markInputRule(regex, type)
|
||||||
return {
|
// })
|
||||||
'Mod-i': () => this.editor.italic()
|
// })
|
||||||
}
|
// .pasteRules(({ type }) => {
|
||||||
}
|
// return ['*', '_'].map(character => {
|
||||||
|
// const regex = VerEx()
|
||||||
|
// .add('(?:^|\\s)')
|
||||||
|
// .beginCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .beginCapture()
|
||||||
|
// .somethingBut(character)
|
||||||
|
// .endCapture()
|
||||||
|
// .find(character)
|
||||||
|
// .endCapture()
|
||||||
|
|
||||||
inputRules() {
|
// return markPasteRule(regex, type)
|
||||||
return ['*', '_'].map(character => {
|
// })
|
||||||
const regex = VerEx()
|
// })
|
||||||
.add('(?:^|\\s)')
|
.create()
|
||||||
.beginCapture()
|
|
||||||
.find(character)
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut(character)
|
|
||||||
.endCapture()
|
|
||||||
.find(character)
|
|
||||||
.endCapture()
|
|
||||||
.endOfLine()
|
|
||||||
|
|
||||||
return markInputRule(regex, this.type)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pasteRules() {
|
|
||||||
return ['*', '_'].map(character => {
|
|
||||||
const regex = VerEx()
|
|
||||||
.add('(?:^|\\s)')
|
|
||||||
.beginCapture()
|
|
||||||
.find(character)
|
|
||||||
.beginCapture()
|
|
||||||
.somethingBut(character)
|
|
||||||
.endCapture()
|
|
||||||
.find(character)
|
|
||||||
.endCapture()
|
|
||||||
|
|
||||||
return markPasteRule(regex, this.type)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
import { Node } from '@tiptap/core'
|
import { Node } from '@tiptap/core'
|
||||||
import { NodeSpec } from 'prosemirror-model'
|
|
||||||
// import ParagraphComponent from './paragraph.vue'
|
// import ParagraphComponent from './paragraph.vue'
|
||||||
|
|
||||||
export default class Paragraph extends Node {
|
export default new Node()
|
||||||
|
.name('paragraph')
|
||||||
name = 'paragraph'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): NodeSpec {
|
|
||||||
return {
|
|
||||||
content: 'inline*',
|
content: 'inline*',
|
||||||
group: 'block',
|
group: 'block',
|
||||||
parseDOM: [{ tag: 'p' }],
|
parseDOM: [{ tag: 'p' }],
|
||||||
toDOM: () => ['p', 0],
|
toDOM: () => ['p', 0],
|
||||||
// toVue: ParagraphComponent,
|
// toVue: ParagraphComponent,
|
||||||
}
|
}))
|
||||||
}
|
.create()
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,8 @@
|
|||||||
import { Node } from '@tiptap/core'
|
import { Node } from '@tiptap/core'
|
||||||
import { NodeSpec } from 'prosemirror-model'
|
|
||||||
|
|
||||||
export default class Text extends Node {
|
export default new Node()
|
||||||
|
.name('text')
|
||||||
name = 'text'
|
.schema(() => ({
|
||||||
|
|
||||||
schema(): NodeSpec {
|
|
||||||
return {
|
|
||||||
group: 'inline',
|
group: 'inline',
|
||||||
}
|
}))
|
||||||
}
|
.create()
|
||||||
|
|
||||||
}
|
|
||||||
@@ -10,14 +10,14 @@ import Heading from '@tiptap/extension-heading'
|
|||||||
|
|
||||||
export default function defaultExtensions() {
|
export default function defaultExtensions() {
|
||||||
return [
|
return [
|
||||||
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(),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -12,14 +12,14 @@ import Heading from '@tiptap/extension-heading'
|
|||||||
|
|
||||||
export function defaultExtensions() {
|
export function defaultExtensions() {
|
||||||
return [
|
return [
|
||||||
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(),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user