Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace defaultOptions with addOptions #2088

Merged
merged 9 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ declare module '@tiptap/core' {
}
}

export const CollaborationAnnotation = Extension.create({
export const CollaborationAnnotation = Extension.create<AnnotationOptions>({
name: 'annotation',

priority: 1000,

defaultOptions: <AnnotationOptions>{
HTMLAttributes: {
class: 'annotation',
},
onUpdate: decorations => decorations,
document: null,
field: 'annotations',
map: null,
instance: '',
addOptions() {
return {
HTMLAttributes: {
class: 'annotation',
},
onUpdate: decorations => decorations,
document: null,
field: 'annotations',
map: null,
instance: '',
}
},

onCreate() {
Expand Down
17 changes: 9 additions & 8 deletions demos/src/Experiments/Commands/Vue/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { Extension } from '@tiptap/core'
import Suggestion from '@tiptap/suggestion'

export default Extension.create({
name: 'mention',
name: 'commands',

defaultOptions: {
suggestion: {
char: '/',
startOfLine: false,
command: ({ editor, range, props }) => {
props.command({ editor, range })
addOptions() {
return {
suggestion: {
char: '/',
command: ({ editor, range, props }) => {
props.command({ editor, range })
},
},
},
}
},

addProseMirrorPlugins() {
Expand Down
6 changes: 4 additions & 2 deletions demos/src/Experiments/Details/Vue/details-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export default Node.create<DetailsSummaryOptions>({

isolating: true,

defaultOptions: {
HTMLAttributes: {},
addOptions() {
return {
HTMLAttributes: {},
}
},

parseHTML() {
Expand Down
6 changes: 4 additions & 2 deletions demos/src/Experiments/Details/Vue/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default Node.create<DetailsOptions>({

// defining: true,

defaultOptions: {
HTMLAttributes: {},
addOptions() {
return {
HTMLAttributes: {},
}
},

parseHTML() {
Expand Down
14 changes: 8 additions & 6 deletions demos/src/Experiments/Embeds/Vue/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ declare module '@tiptap/core' {
}
}

export default Node.create({
export default Node.create<IframeOptions>({
name: 'iframe',

group: 'block',

atom: true,

defaultOptions: <IframeOptions>{
allowFullscreen: true,
HTMLAttributes: {
class: 'iframe-wrapper',
},
addOptions() {
return {
allowFullscreen: true,
HTMLAttributes: {
class: 'iframe-wrapper',
},
}
},

addAttributes() {
Expand Down
6 changes: 4 additions & 2 deletions demos/src/Experiments/Figure/Vue/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export const inputRegex = /!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\)/
export const Figure = Node.create<FigureOptions>({
name: 'figure',

defaultOptions: {
HTMLAttributes: {},
addOptions() {
return {
HTMLAttributes: {},
}
},

group: 'block',
Expand Down
6 changes: 4 additions & 2 deletions demos/src/Experiments/GenericFigure/Vue/figcaption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Node, mergeAttributes } from '@tiptap/core'
export const Figcaption = Node.create({
name: 'figcaption',

defaultOptions: {
HTMLAttributes: {},
addOptions() {
return {
HTMLAttributes: {},
}
},

content: 'inline*',
Expand Down
6 changes: 4 additions & 2 deletions demos/src/Experiments/GenericFigure/Vue/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { Plugin } from 'prosemirror-state'
export const Figure = Node.create({
name: 'figure',

defaultOptions: {
HTMLAttributes: {},
addOptions() {
return {
HTMLAttributes: {},
}
},

group: 'block',
Expand Down
8 changes: 5 additions & 3 deletions demos/src/Experiments/Linter/Vue/extension/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export interface LinterOptions {
plugins: Array<typeof LinterPlugin>,
}

export const Linter = Extension.create({
export const Linter = Extension.create<LinterOptions>({
name: 'linter',

defaultOptions: <LinterOptions>{
plugins: [],
addOptions() {
return {
plugins: [],
}
},

addProseMirrorPlugins() {
Expand Down
12 changes: 7 additions & 5 deletions demos/src/Experiments/TrailingNode/Vue/trailing-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export interface TrailingNodeOptions {
export const TrailingNode = Extension.create<TrailingNodeOptions>({
name: 'trailingNode',

defaultOptions: {
node: 'paragraph',
notAfter: [
'paragraph',
],
addOptions() {
return {
node: 'paragraph',
notAfter: [
'paragraph',
],
}
},

addProseMirrorPlugins() {
Expand Down
8 changes: 5 additions & 3 deletions docs/guide/custom-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ All settings can be configured through the extension anyway, but if you want to
import Heading from '@tiptap/extension-heading'

const CustomHeading = Heading.extend({
defaultOptions: {
...Heading.options,
levels: [1, 2, 3],
addOptions() {
return {
...this.parent(),
levels: [1, 2, 3],
}
},
})
```
Expand Down
6 changes: 4 additions & 2 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export interface CustomExtensionOptions {
}

const CustomExtension = Extension.create<CustomExtensionOptions>({
defaultOptions: {
awesomeness: 100,
addOptions() {
return {
awesomeness: 100,
}
},
})
```
Expand Down
2 changes: 1 addition & 1 deletion docs/overview/upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import { Node } from '@tiptap/core'

const CustomExtension = Node.create({
name: 'custom_extension',
defaultOptions: {
addOptions() {
},
addAttributes() {
Expand Down
44 changes: 42 additions & 2 deletions packages/core/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ declare module '@tiptap/core' {
*/
defaultOptions?: Options,

/**
* Default Options
*/
addOptions?: (this: {
name: string,
parent: Exclude<ParentConfig<ExtensionConfig<Options, Storage>>['addOptions'], undefined>,
}) => Options,

/**
* Default Storage
*/
addStorage?: (this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addGlobalAttributes'],
parent: Exclude<ParentConfig<ExtensionConfig<Options, Storage>>['addStorage'], undefined>,
}) => Storage,

/**
Expand Down Expand Up @@ -278,15 +286,32 @@ export class Extension<Options = any, Storage = any> {
}

this.name = this.config.name

if (config.defaultOptions) {
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`)
}

// TODO: remove `addOptions` fallback
this.options = this.config.defaultOptions

if (this.config.addOptions) {
this.options = callOrReturn(getExtensionField<AnyConfig['addOptions']>(
this,
'addOptions',
{
name: this.name,
},
))
}

this.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
this,
'addStorage',
{
name: this.name,
options: this.options,
},
))
)) || {}
}

static create<O = any, S = any>(config: Partial<ExtensionConfig<O, S>> = {}) {
Expand Down Expand Up @@ -323,10 +348,25 @@ export class Extension<Options = any, Storage = any> {
? extendedConfig.name
: extension.parent.name

if (extendedConfig.defaultOptions) {
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`)
}

// TODO: remove `addOptions` fallback
extension.options = extendedConfig.defaultOptions
? extendedConfig.defaultOptions
: extension.parent.options

if (extendedConfig.addOptions) {
extension.options = callOrReturn(getExtensionField<AnyConfig['addOptions']>(
extension,
'addOptions',
{
name: extension.name,
},
))
}

extension.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
extension,
'addStorage',
Expand Down
46 changes: 43 additions & 3 deletions packages/core/src/Mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ declare module '@tiptap/core' {
*/
defaultOptions?: Options,

/**
* Default Options
*/
addOptions?: (this: {
name: string,
parent: Exclude<ParentConfig<MarkConfig<Options, Storage>>['addOptions'], undefined>,
}) => Options,

/**
* Default Storage
*/
addStorage?: (this: {
addStorage?: (this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options, Storage>>['addGlobalAttributes'],
parent: Exclude<ParentConfig<MarkConfig<Options, Storage>>['addStorage'], undefined>,
}) => Storage,

/**
Expand Down Expand Up @@ -392,15 +400,32 @@ export class Mark<Options = any, Storage = any> {
}

this.name = this.config.name

if (config.defaultOptions) {
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`)
}

// TODO: remove `addOptions` fallback
this.options = this.config.defaultOptions

if (this.config.addOptions) {
this.options = callOrReturn(getExtensionField<AnyConfig['addOptions']>(
this,
'addOptions',
{
name: this.name,
},
))
}

this.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
this,
'addStorage',
{
name: this.name,
options: this.options,
},
))
)) || {}
}

static create<O = any, S = any>(config: Partial<MarkConfig<O, S>> = {}) {
Expand Down Expand Up @@ -437,10 +462,25 @@ export class Mark<Options = any, Storage = any> {
? extendedConfig.name
: extension.parent.name

if (extendedConfig.defaultOptions) {
console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`)
}

// TODO: remove `addOptions` fallback
extension.options = extendedConfig.defaultOptions
? extendedConfig.defaultOptions
: extension.parent.options

if (extendedConfig.addOptions) {
extension.options = callOrReturn(getExtensionField<AnyConfig['addOptions']>(
extension,
'addOptions',
{
name: extension.name,
},
))
}

extension.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
extension,
'addStorage',
Expand Down
Loading