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 context option to SvelteNodeViewRenderer #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kuruczg444
Copy link

Currently there is no way to make contexts defined outside the Editor instance available for custom extensions.

With this little change, it's now possible to use Svelte's getAllContexts function to pass them to the custom extensions. Here's an example:

Editor.svelte

<script lang="ts">
  import { getAllContexts, onMount } from 'svelte';
  import type { Readable } from 'svelte/store';
  import { createEditor, Editor, EditorContent } from 'svelte-tiptap';
  import StarterKit from '@tiptap/starter-kit';
  import MyCustomExtension from './MyCustomExtension';

  let editor = $state() as Readable<Editor>;

  const allContexts = getAllContexts();

  onMount(() => {
    editor = createEditor({
      extensions: [StarterKit, MyCustomExtension(allContexts)],
      content: `Hello world!`,
    });
  });
</script>

<EditorContent editor={$editor} />

MyCustomExtension.ts

import { Node } from '@tiptap/core';
import { SvelteNodeViewRenderer } from 'svelte-tiptap';
import MyCustomComponent from './MyCustomComponent.svelte';

export default (context: Map<any, any>) => Node.create({
  // ...
  addNodeView() {
    return SvelteNodeViewRenderer(MyCustomComponent, { context });
  },
  // ...
})

@@ -26,6 +26,7 @@ export interface SvelteNodeViewRendererOptions extends NodeViewRendererOptions {
update: ((props: RendererUpdateProps) => boolean) | null;
as?: string;
attrs?: AttrProps;
context?: Map<any, any>;
Copy link
Owner

Choose a reason for hiding this comment

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

haven't checkout the code yet. shouldn't this be string, unknown?

Copy link
Author

Choose a reason for hiding this comment

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

I used the default ReturnType of getAllContexts: https://svelte.dev/docs/svelte/svelte#getAllContexts which is Map<any, any>.

As contexts are not directly accessed anyway, I believe the type here is not really relevant other than it being a Map.

However, string is definitely not correct as Map keys in Svelte can also be Symbols.

The better pattern to access contexts are documented in the Svelte docs

Copy link
Owner

Choose a reason for hiding this comment

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

Mentioned just for the sake of eslint. planning to import some shared configuration where any is explicitly avoided. might have to exclude this line, hence said

Copy link
Author

Choose a reason for hiding this comment

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

Maybe ReturnType<typeof getAllContexts> could be used to not use any any.

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.

2 participants