• Stars
    star
    219
  • Rank 181,133 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Svelte components for tiptap v2

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

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

This enables rendering Svelte Components as NodeViews.

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 'classnames';
  import { NodeViewWrapper } from 'svelte-tiptap';

  export let node: NodeViewProps['node'];
  export let updateAttributes: NodeViewProps['updateAttributes'];
  export let selected: NodeViewProps['selected'] = false;

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

<NodeViewWrapper class={cx('svelte-component', { selected })}>
  <span class="label">Svelte Component</span>

  <div class="content">
    <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.

More Repositories

1

ngx-editor

πŸ–‹οΈ Rich Text Editor for angular using ProseMirror
TypeScript
442
star
2

ngx-tiptap

Angular bindings for tiptap v2
TypeScript
112
star
3

marked-react

βš›οΈ Render Markdown as React components
TypeScript
64
star
4

action-eslint

βœ… GitHub Action that runs ESLint on files changed in a Pull Request
TypeScript
42
star
5

react-layout-masonry

Responsive masonry layout for React
TypeScript
29
star
6

browser-dtector

🌐 A Javascript library to detect browser, version and platform
TypeScript
27
star
7

eslint-plugin-file-progress

Eslint plugin to print file progress
JavaScript
26
star
8

prosemirror-codemirror-6

Prosemirror with Codemirror 6 (demo)
TypeScript
14
star
9

vscode-scss-formatter

A Visual Studio Code Extension to format SCSS files
TypeScript
9
star
10

angularjs-toast

🍻 A Simple toast notification service for AngularJS pages
JavaScript
7
star
11

angular-paginator

πŸ“– Pagination Component for Angular applications
TypeScript
6
star
12

ngx-notifier

πŸ”” A Simple Notification Service for Angular applications
TypeScript
4
star
13

merge-json-webpack-plugin

οΉ› Webpack plugin to merge multiple json files into one ﹜
JavaScript
3
star
14

validator.fn

A simple function to aid form validations
JavaScript
2
star
15

ng-browser-detector

πŸ–₯ AngularJs service to detect platform, browser and version
JavaScript
2
star
16

unique-names-generator

Go Package to generate random and unique names
Go
2
star
17

ui-router-breadcrumbs

🍞 Breadcrumbs for AngularJS pages using angular-ui-router
CoffeeScript
2
star
18

capillaries

⚑️ Javascript Events and Hooks
TypeScript
2
star
19

angularjs-file-model

AngularJS directive to support NgModel for HTML input file types
JavaScript
2
star
20

pkg.json-webpack-plugin

πŸ“¦ Loads package.json into webpack app as an environment variable
JavaScript
1
star
21

copy-asset-in-memory-webpack-plugin

Copy assets in webpack memory
JavaScript
1
star
22

react-boilerplate

βš›οΈ Minimal React Webpack Boilerplate
JavaScript
1
star
23

gatsby.js-detector

Extension to detect sites built with Gatsby.js
JavaScript
1
star
24

action-archiver

Github action to generate tar/zip archives
TypeScript
1
star
25

hugo-multilingual-boilerplate

Hugo multilingual boilerplate to demonstrate internationalization (i18n) and multilingualization (m17n)
HTML
1
star
26

replace-asset-name-webpack-plugin

Webpack Plugin to replace blocks with asset name matching given regex or string
JavaScript
1
star