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

Strongly typed extension storage #6114

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions .changeset/happy-squids-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@tiptap/core": minor
"@tiptap/extension-character-count": minor
"@tiptap/extension-collaboration": minor
"@tiptap/extension-collaboration-cursor": minor
---

Allow the type of the extensionStorage field of the Editor class to be customizable
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ type CustomStorage = {
foo: number,
}

declare module '@tiptap/core' {
interface ExtensionStorage {
custom: CustomStorage;
}
}

export const CustomExtension = Extension.create<any, CustomStorage>({
name: 'custom',

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getTextSerializersFromSchema } from './helpers/getTextSerializersFromSc
import { isActive } from './helpers/isActive.js'
import { isNodeEmpty } from './helpers/isNodeEmpty.js'
import { resolveFocusPosition } from './helpers/resolveFocusPosition.js'
import { ExtensionStorage } from './index.js'
import { NodePos } from './NodePos.js'
import { style } from './style.js'
import {
Expand Down Expand Up @@ -64,7 +65,7 @@ export class Editor extends EventEmitter<EditorEvents> {
*/
public isInitialized = false

public extensionStorage: Record<string, any> = {}
public extensionStorage: ExtensionStorage | Record<string, any> = {}

public options: EditorOptions = {
element: document.createElement('div'),
Expand Down Expand Up @@ -129,7 +130,7 @@ export class Editor extends EventEmitter<EditorEvents> {
/**
* Returns the editor storage.
*/
public get storage(): Record<string, any> {
public get storage(): ExtensionStorage | Record<string, any> {
return this.extensionStorage
}

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export * from './utilities/index.js'
// eslint-disable-next-line
export interface Commands<ReturnType = any> {}

// eslint-disable-next-line
export interface ExtensionStorage {}

// eslint-disable-next-line
export interface ExtensionConfig<Options = any, Storage = any> {}

Expand Down
6 changes: 6 additions & 0 deletions packages/extension-character-count/src/character-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export interface CharacterCountStorage {
words: (options?: { node?: ProseMirrorNode }) => number
}

declare module '@tiptap/core' {
interface ExtensionStorage {
characterCount: CharacterCountStorage;
}
}

/**
* This extension allows you to count the characters and words of your document.
* @see https://tiptap.dev/api/extensions/character-count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ declare module '@tiptap/core' {
user: (attributes: Record<string, any>) => ReturnType,
}
}

interface ExtensionStorage {
collaborationCursor: CollaborationCursorStorage
}
}

const awarenessStatesToArray = (states: Map<number, Record<string, any>>) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/extension-collaboration/src/collaboration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ declare module '@tiptap/core' {
redo: () => ReturnType;
};
}

interface ExtensionStorage {
collaboration: CollaborationStorage;
}
}

export interface CollaborationStorage {
Expand Down
Loading