feat: add priority option to extensions

This commit is contained in:
Philipp Kühn 2021-04-07 18:29:16 +02:00
parent 6d83bef97d
commit bb1ae659a4
12 changed files with 64 additions and 11 deletions

View File

@ -74,6 +74,9 @@ export default {
Highlight,
TaskList,
TaskItem,
Collaboration.configure({
document: ydoc,
}),
CollaborationCursor.configure({
provider: this.provider,
user: this.currentUser,
@ -81,9 +84,6 @@ export default {
this.users = users
},
}),
Collaboration.configure({
document: ydoc,
}),
],
})

View File

@ -33,6 +33,9 @@ export default {
Document,
Paragraph,
Text,
Collaboration.configure({
document: ydoc,
}),
CollaborationCursor.configure({
provider: this.provider,
user: {
@ -40,9 +43,6 @@ export default {
color: '#f783ac',
},
}),
Collaboration.configure({
document: ydoc,
}),
],
})
},

View File

@ -16,6 +16,7 @@ import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Link from '@tiptap/extension-link'
import Bold from '@tiptap/extension-bold'
export default {
components: {
@ -34,11 +35,12 @@ export default {
Document,
Paragraph,
Text,
Bold,
Link,
],
content: `
<p>
Wow, this editor has support for links to the whole <a href="https://en.wikipedia.org/wiki/World_Wide_Web">world wide web</a>. We tested a lot of URLs and I think you can add *every URL* you want. Isnt that cool? Lets try <a href="https://statamic.com/">another one!</a> Yep, seems to work.
Wow, this editor has support for links to the whole <strong><a href="https://en.wikipedia.org/wiki/World_Wide_Web">world wide web</a></strong>. We tested a lot of URLs and I think you can add *every URL* you want. Isnt that cool? Lets try <a href="https://statamic.com/">another one!</a> Yep, seems to work.
</p>
<p>
By default every link will get a \`rel="noopener noreferrer nofollow"\` attribute. Its configurable though.

View File

@ -158,15 +158,15 @@ const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'example-document'
const editor = new Editor({
extensions: [
Collaboration.configure({
document: ydoc,
}),
// Register the collaboration cursor extension
CollaborationCursor.configure({
provider: provider,
name: 'Cyndi Lauper',
color: '#f783ac',
}),
Collaboration.configure({
document: ydoc,
}),
// …
],
})

View File

@ -39,6 +39,17 @@ The extension name is used in a whole lot of places and changing it isnt too
The extension name is also part of the JSON. If you [store your content as JSON](/guide/output#option-1-json), you need to change the name there too.
### Priority
With the priority you can specify the order of the extensions. This has an impact on the schema and ProseMirror plugins.
```js
import Link from '@tiptap/extension-link'
const CustomLink = Link.extend({
priority: 1000,
})
```
### Settings
All settings can be configured through the extension anyway, but if you want to change the default settings, for example to provide a library on top of tiptap for other developers, you can do it like that:

View File

@ -16,6 +16,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@ -180,6 +185,7 @@ export class Extension<Options = any> {
config: ExtensionConfig = {
name: 'extension',
priority: 100,
defaultOptions: {},
}

View File

@ -25,7 +25,7 @@ export default class ExtensionManager {
constructor(extensions: Extensions, editor: Editor) {
this.editor = editor
this.extensions = extensions
this.extensions = this.sort(extensions)
this.schema = getSchema(this.extensions)
this.extensions.forEach(extension => {
@ -77,6 +77,22 @@ export default class ExtensionManager {
})
}
private sort(extensions: Extensions) {
const defaultPriority = 100
return extensions.sort((a, b) => {
if ((a.config.priority || defaultPriority) > (b.config.priority || defaultPriority)) {
return -1
}
if ((a.config.priority || defaultPriority) < (b.config.priority || defaultPriority)) {
return 1
}
return 0
})
}
get commands(): RawCommands {
return this.extensions.reduce((commands, extension) => {
const context = {

View File

@ -21,6 +21,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@ -254,6 +259,7 @@ export class Mark<Options = any> {
config: MarkConfig = {
name: 'mark',
priority: 100,
defaultOptions: {},
}

View File

@ -26,6 +26,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@ -312,6 +317,7 @@ export class Node<Options = any> {
config: NodeConfig = {
name: 'node',
priority: 100,
defaultOptions: {},
}

View File

@ -31,6 +31,8 @@ const awarenessStatesToArray = (states: Map<number, { [key: string]: any }>) =>
export const CollaborationCursor = Extension.create<CollaborationCursorOptions>({
name: 'collaborationCursor',
priority: 1000,
defaultOptions: {
provider: null,
user: {

View File

@ -38,6 +38,8 @@ export const pasteRegexWithBrackets = /(?:\()https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%
export const Link = Mark.create<LinkOptions>({
name: 'link',
priority: 1000,
inclusive: false,
defaultOptions: {

View File

@ -20,6 +20,8 @@ declare module '@tiptap/core' {
export const Paragraph = Node.create<ParagraphOptions>({
name: 'paragraph',
priority: 1000,
defaultOptions: {
HTMLAttributes: {},
},