Giter Club home page Giter Club logo

svelte-tiptap's Introduction

svelte-tiptap

Svelte components for tiptap v2

Tests NPM Version Total Downloads Monthly Downloads License

Installation

npm i svelte-tiptap
# or
yarn add svelte-tiptap

Note

This package just provides components for svelte. For configuring/customizing the editor, refer tiptap's official documentation.

For any issues with the editor. You may need to open the issue on tiptap's repository

You can find some examples for the editor here

Usage

A Simple editor.

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

  let editor: Readable<Editor>;

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

<EditorContent editor={$editor} />

Refer https://www.tiptap.dev/api/commands/ for available commands

Extensions

Refer: https://www.tiptap.dev/api/extensions

Floating menu

This will make a contextual menu appear near a selection of text.

The markup and styling are totally up to you.

<script lang="ts">
  import { EditorContent, FloatingMenu } from 'svelte-tiptap';

  // ...create the editor instance on mount
</script>

<EditorContent editor={$editor} />
<FloatingMenu editor={$editor} />

Refer: https://www.tiptap.dev/api/extensions/floating-menu

Bubble Menu

This will make a contextual menu appear near a selection of text. Use it to let users apply marks to their text selection.

The markup and styling are totally up to you.

<script lang="ts">
  import { EditorContent, BubbleMenu } from 'svelte-tiptap';

  // ...create the editor instance on mount
</script>

<EditorContent editor={$editor} />
<BubbleMenu editor={$editor} />

Refer: https://www.tiptap.dev/api/extensions/bubble-menu

SvelteNodeViewRenderer

SvelteNodeViewRenderer enables rendering Svelte Components as NodeViews. The following is an example for creating a counter component

Create a Node Extension

import { Node, mergeAttributes } from '@tiptap/core';
import { SvelteNodeViewRenderer } from 'svelte-tiptap';

import CounterComponent from './Counter.svelte';

export const SvelteCounterExtension = Node.create({
  name: 'svelteCounterComponent',
  group: 'block',
  atom: true,
  draggable: true, // Optional: to make the node draggable
  inline: false,

  addAttributes() {
    return {
      count: {
        default: 0,
      },
    };
  },

  parseHTML() {
    return [{ tag: 'svelte-counter-component' }];
  },

  renderHTML({ HTMLAttributes }) {
    return ['svelte-counter-component', mergeAttributes(HTMLAttributes)];
  },

  addNodeView() {
    return SvelteNodeViewRenderer(CounterComponent);
  },
});

Create a Component

<script lang="ts">
  import type { NodeViewProps } from '@tiptap/core';
  import cx from 'clsx';
  import { NodeViewWrapper } from 'svelte-tiptap';

  export let node: NodeViewProps['node'];
  export let updateAttributes: NodeViewProps['updateAttributes'];

  const handleClick = () => {
    updateAttributes({ count: node.attrs.count + 1 });
  };
</script>

<NodeViewWrapper>
  <span>Svelte Component</span>

  <div>
    <button on:click={handleClick} type="button">
      This button has been clicked {node.attrs.count} times.
    </button>
  </div>
</NodeViewWrapper>

Use the extension

import { onMount, onDestroy } from 'svelte';
import type { Readable } from 'svelte/store';
import { Editor, EditorContent } from 'svelte-tiptap';
import StarterKit from '@tiptap/starter-kit';

import { SvelteCounterExtension } from './SvelteExtension';

let editor: Readable<Editor>;

onMount(() => {
  editor = createEditor({
    extensions: [StarterKit, SvelteCounterExtension],
    content: `
        <p>This is still the text editor you’re used to, but enriched with node views.</p>
        <svelte-counter-component count="0"></svelte-counter-component>
        <p>Did you see that? That’s a Svelte component. We are really living in the future.</p>
      `,
  });
});

Access/Update Attributes

Refer https://www.tiptap.dev/guide/node-views/react/#all-available-props for the list of all available attributes. You can access them like

import type { NodeViewProps } from '@tiptap/core';

export let node: NodeViewProps['node'];
export let updateAttributes: NodeViewProps['updateAttributes'];
// ...define other props as needed.

// update attributes
const handleClick = () => {
  updateAttributes({ count: node.attrs.count + 1 });
};

Dragging

To make your node views draggable, set draggable: true in the extension and add data-drag-handle to the DOM element that should function as the drag handle.

Adding a content editable

There is another action called editable which helps you adding editable content to your node view. Here is an example.

<script lang="ts">
  import { NodeViewWrapper, NodeViewContent } from 'svelte-tiptap';
</script>

<NodeViewWrapper class="svelte-component">
  <span class="label" contenteditable="false">Svelte Editable Component</span>

  <!-- Content is inserted here -->
  <NodeViewContent />
</NodeViewWrapper>

The NodeViewWrapper and NodeViewContent components render a <div> HTML tag (<span> for inline nodes), but you can change that. For example <NodeViewContent as="p"> should render a paragraph. One limitation though: That tag must not change during runtime.

Refer: https://www.tiptap.dev/guide/node-views/react/#adding-a-content-editable

Contributing

All types of contributions are welcome. See CONTRIBUTING.md to get started.

svelte-tiptap's People

Contributors

andheller avatar dependabot[bot] avatar douganderson444 avatar leerobert avatar seo-rii avatar sibiraj-s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

svelte-tiptap's Issues

Could not resolve "@tiptap/extension-floating-menu"

node_modules/svelte-tiptap/dist/FloatingMenu.svelte:31:35:
31 │ import { FloatingMenuPlugin } from "@tiptap/extension-floating-menu";
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can mark the path "@tiptap/extension-floating-menu" as external to exclude it from the bundle,
which will remove this error.

10:21:05 AM [vite] error while updating dependencies:
Error: Build failed with 1 error:
node_modules/svelte-tiptap/dist/FloatingMenu.svelte:31:35: ERROR: Could not resolve "@tiptap/extension-floating-menu"
at failureErrorWithLog (/Users/jakes/PProjects/store254_site/node_modules/esbuild/lib/main.js:1649:15)
at /Users/jala/Perojects/store254_site/node_modules/esbuild/lib/main.js:1058:25
at /Users/jala/Perojects/store254_site/node_modules/esbuild/lib/main.js:1525:9
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Am getting this whenever i try to add the first example in ReadMe in my svelte project

Critical error when trying to use with SvelteKit 1.0

when trying to use this library with the full release, the following error crops up:

Cannot read properties of undefined (reading 'Editor')
TypeError: Cannot read properties of undefined (reading 'Editor')
    at eval (/node_modules/svelte-tiptap/Editor.js:5:42)

perhaps this would be fixed by upgrading this library to SvelteKit 1.0, as well?

Remove default styles

Tiptap is a headless solution, but this library injects .ProseMirror styles. Can this be disabled? I'd argue that it should be opt-in, not on by default.

No idea what to do...

I am trying to add the floating and bubble menus:

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

  let editor: Readable<Editor>;

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

<EditorContent editor={$editor} />
<FloatingMenu editor={$editor} />
<BubbleMenu editor={$editor} />

...and I get the following error:

Error: Missing editor instance.
    at eval (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/node_modules/svelte-tiptap/dist/FloatingMenu.svelte:20:9)
    at Object.$$render (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at eval (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/src/library/editor/SvelteTipTap.svelte:31:251)
    at Object.$$render (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at eval (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/src/routes/tiptap/+page.svelte:20:136)
    at Object.$$render (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at Object.default (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/.svelte-kit/generated/root.svelte:102:133)
    at eval (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/src/routes/+layout.svelte:34:487)
    at Object.$$render (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/node_modules/svelte/src/runtime/internal/ssr.js:174:16)
    at eval (/Users/rchrdnsh/Code/Svelte/__active-projects/lstv/.svelte-kit/generated/root.svelte:65:129)

...but I am passing in the editor store, correct?

Don't know what to do next...

Thanks in advance :-)

Bubble menu glitches when the editor changes positions

Hey, first of all thanks for the great work

I am trying to wrap my head around bubble menu glitch, that you can check out here https://svelte.dev/repl/89cf6d5d4017435b8b7474c088057ff4?version=3.46.4

If there are many instances of tiptap and they get remounted (like if the positions change) the bubble menu gets rendered with a style "visibility: visible;" (if you click on the tiptap editor after that it goes away)

I can't find a way to make it not do that :(

Packaging Upgrades

Hey this is a really cool TipTap extension, I'm a big fan of Svelte and noticed most of their TipTap stuff is React & Vue.

I have noticed that the current build of this repo doesn't add .js extensions when the TS is transformed, which is messing up some of my use of this package.

Do you mind if I submit a Pull Request to updated the packaging pipeline to fix this? I've already made the changes in my fork.

Help with the `isActive` reactivity

Hi.

First and foremost, thank you so much for the project. It has been a great joy using it.

That said, I ran into some trouble trying to create the style toggle items.

From the examples, you create individual functions, say:

const toggleHeading = (level: 1 | 2) => {
		return () => {
			$editor.chain().focus().toggleHeading({ level }).run();
		};
	};

and then create a button to toggle:

<button
		type="button"
		class={cx('hover:bg-black hover:text-white w-7 h-7 rounded', {
			'bg-black text-white': isActive('heading', { level: 1 })
		})}
		on:click={toggleHeading(1)}
	>
			H1
</button>

Now, given that I want to create several menu items, I don't want the trouble of repeating the above snippet2 for every item.

So, I created an array as such:

const menuItems = [
		{
			name: 'heading 1',
			command: toggleHeading(1),
			content: 'Heading 1',
			icon: Heading1,
			active: () => isActive('heading', { level: 1 })
		},
		{
			name: 'heading 2',
			command: toggleHeading(2),
			content: 'Heading 2',
			icon: Heading2,
			active: () => isActive('heading', { level: 2 })
		},
		{
			name: 'heading 3',
			command: toggleHeading(3),
			content: 'Heading 3',
			icon: Heading3,
			active: () => isActive('heading', { level: 3 })
		},
		{
			name: 'heading 4',
			command: toggleHeading(4),
			content: 'Heading 4',
			icon: Heading4,
			active: () => isActive('heading', { level: 4 })
		},
		{
			name: 'heading 5',
			command: toggleHeading(5),
			content: 'Heading 5',
			icon: Heading5,
			active: () => isActive('heading', { level: 5 })
		},
		{
			name: 'heading 6',
			command: toggleHeading(6),
			content: 'Heading 6',
			icon: Heading6,
			active: () => isActive('heading', { level: 6 })
		},
		{
			name: 'bold',
			command: toggleBold,
			content: 'Bold',
			icon: Bold,
			active: () => isActive('bold')
		},
		{
			name: 'italic',
			command: toggleItalic,
			content: 'Italic',
			icon: Italic,
			active: () => isActive('italic')
		},
		{
			name: 'code',
			command: toggleCodeBlock,
			content: 'Code',
			icon: Code,
			active: () => isActive('codeBlock')
		},
		{
			name: 'strike',
			command: toggleStrike,
			content: 'Strike',
			icon: Strikethrough,
			active: () => isActive('strike')
		},
		{
			name: 'bulletList',
			command: toggleBulletList,
			content: 'Bullet List',
			icon: List,
			active: () => isActive('bulletList')
		},
		{
			name: 'orderedList',
			command: toggleOrderedList,
			content: 'Ordered List',
			icon: ListOrdered,
			active: () => isActive('orderedList')
		},
		{
			name: 'paragraph',
			command: setParagraph,
			content: 'Paragraph',
			icon: Pilcrow,
			active: () => isActive('paragraph')
		}
	];

so that I can iterate in the markup like so:

	{#if editor && editable}
	<TooltipProvider>
		<ul class="flex p-2 border-2 border-b-0 rounded-t-md">
			{#each menuItems as item (item.name)}
				<li>
					<Tooltip>
						<TooltipTrigger>
							<button
								class={cn(
									'hover:bg-black hover:text-white w-7 h-7 rounded grid place-items-center',
									{
										'bg-black text-white': item.active()
									}
								)}
								on:click={item.command}
							>
								<svelte:component this={item.icon} class="w-4 h-4" />
								<span class="sr-only">{item.content}</span>
							</button>
						</TooltipTrigger>
						<TooltipContent>
							<p>{item.content}</p>
						</TooltipContent>
					</Tooltip>
				</li>
			{/each}
		</ul>
	</TooltipProvider>
	{/if}

The problem I ran into, is that when doing it like that, the isActive store doesn't seem to work. What I mean is:

  • if, say, H1, and B are active, then the background doesn't register.
  • Also, the P that usually gets active whenever you jump to a new line, doesn't seem to register as well.

Any help is greatly appreciated.

Here is a link to a detailed Gist

Thanks.

Minimal example in readme does not work without one extension

Hi,

I use SvelteKit and need to add a minimum of one extension in order for it to load. The readme example does not include any extensions.

Until that, I got the error Uncaught (in promise) RangeError: Schema is missing its top node type ('doc').

My working code:

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

	let editor: Readable<Editor>;

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

<EditorContent editor={$editor} />

Set `tsconfig` compiler target to ES2021 to prevent `SvelteNodeViewRenderer` to break

I've spent hours trying to get Custom Node Extensions to work before I finally stumbled upon the solution through a comment in the project's tsconfig file. I'm raising an issue about it in case someone else encounters the same problem.

To prevent SvelteNodeViewRenderer from breaking when using Custom Node Extensions, you need to adjust the compilerOptions.target in your project's tsconfig file to ES2021 (instead of the default esnext).

{
  "compilerOptions": {
    "target": "ES2021" // ES2022 or later breaks SvelteNodeViewRenderer
  }
}

For reference, you can track the underlying issue from tiptap repo (tiptap issue #4906).

What is the purpose of svelte-component-selected?

Hi! In the README there is

class={cx('svelte-component', { selected })}

What is the purpose of this? Is it mandatory and tied to some tiptap-functionality (events etc), or is it just something for styling in this example? (if so, perhaps remove it so that it does not confuses)

Whole node is draggable

Hi,

Appologies if this is more of a tiptap core question, but your docs state:

Dragging
To make your node views draggable, set draggable: true in the extension and add data-drag-handle to the DOM element that should function as the drag handle.

When I add draggable: true then the whole node is draggable, but this implies that I can add data-drag-handle to s single node and have this only draggable but that doesn't seem to function as expected.

Any Ideas?

Chris

Support of Mention/Suggestion extension

Similar to BubbleMenu, the Suggestion/Mention extension require injecting in a custom component. I played around with trying to get SvelteRender to work injecting a component in but I'm not smart enough with Svelte to figure this out.

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'element') at init

I get this error when trying to implement the plugin in my Sveltekit Project (Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'element') at init_).

The code comes from the readme with a few exceptions, because I don't use Typescript:

<script >
    import { onMount, onDestroy } from 'svelte';
    import { createEditor, EditorContent } from 'svelte-tiptap';
    import StarterKit from "@tiptap/starter-kit";

    let editor

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

<EditorContent editor={editor} />

handling unused props with `SvelteNodeViewRenderer`

When adding node views with SvelteNodeViewRenderer, the node component is passed all of the NodeViewProps as individual props which creates unknown prop errors like this:

<NodeComponent> was created with unknown prop 'editor'

Is it possible to pass only the props which are exported by the component? Is there another recommended way to handle NodeViewProps keys that are not used?

Editable component doesn't allow backspace or most editing operations if initial content is empty.

To reproduce this issue, change the code on the "advanced editor" demo page from

<svelte-editable-component>This is editable</svelte-editable-component>

to

<svelte-editable-component></svelte-editable-component>

Then try to edit the editable region. You'll find that you can't navigate backward with the left arrow key. If you look at the DOM, you'll see that the custom content is being added to the id="editable-component" element, rather than the span inside that element.

how to dynamically update the editor content ?

Hi,
This is more a question than an issue but I would like to update the content on the editor on the fly, while the user is typing by sending the editorContent to an external process and getting back enriched text (with formatting and metadata).

For example:
original content:

[{
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Example Text"
        }
      ]
    }]

content to reinject:

[{
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Example"
        },
        {
          "type": "strong",
          "text": "Text"
        },
      ]
    }]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.