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

Add extension storage #2069

Merged
merged 9 commits into from Oct 22, 2021
Merged

Add extension storage #2069

merged 9 commits into from Oct 22, 2021

Conversation

philippkuehn
Copy link
Contributor

@philippkuehn philippkuehn commented Oct 21, 2021

At some point you probably want to save some data within your extension instance or want to provide some methods that are accessible outside of the extension. With this PR it’s possible now.

The stored data is mutable. You can access it within the extension under this.storage.

import { Extension } from '@tiptap/core'

const CustomExtension = Extension.create({
  name: 'customExtension',

  addStorage() {
    return {
      awesomeness: 100,
    }
  },

  onUpdate() {
    this.storage.awesomeness += 1
  },
})

When using Vue or React, this storage is reactive within your templates.

Outside the extension you have access via editor.storage. Make sure that each extension has a unique name.

const editor = new Editor({
  extensions: [
    CustomExtension,
  ],
})

const awesomeness = editor.storage.customExtension.awesomeness

When using TypeScript you can define a type for your storage.

import { Extension } from '@tiptap/core'

export interface CustomExtensionStorage {
  awesomeness: number,
}

const CustomExtension = Extension.create<{}, CustomExtensionStorage>({
  addStorage() {
    return {
      awesomeness: 100,
    }
  },
})

Global storage types can’t be inferred so you have to give a hint:

const storage = editor.storage.customExtension as CustomExtensionStorage

@philippkuehn philippkuehn merged commit 7ffabf2 into main Oct 22, 2021
@philippkuehn philippkuehn deleted the feature/extension-storage branch October 22, 2021 06:53
@hanspagel hanspagel mentioned this pull request Oct 26, 2021
10 tasks
Comment on lines +21 to +22
requestAnimationFrame(() => {
requestAnimationFrame(() => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philippkuehn is there a reason why we need to call requestAnimationFrame within a requestAnimationFrame ? Why is 1 requestAnimationFrame not sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always do a double RAF because of this. Maybe it’s not needed here. Is there any issue with that?

andrewlu0 pushed a commit to trybaseplate/tiptap that referenced this pull request Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants