svelte-lexical

repository·master·Indexed 20 days ago

https://github.com/umaranis/svelte-lexical

Svelte bindings and high-level components for the Lexical text editor framework. It enables the creation of rich-text editors in Svelte and SvelteKit applications, featuring a plugin system and support for real-time collaboration using Yjs and y-websocket.

Tokens
26.1K
Snippets
83
Records
117
Agent score
70%

What's inside svelte-lexical

  1. Overview of Qalam

    master

    Qalam is a distraction-free notes application built using svelte-lexical. It is designed to run either as a web application in a browser or as a desktop application via Tauri.

    Key Features

    • Rich-text Editor: Supports headings, lists, checklists, links, tables, images, code blocks (with syntax highlighting), and embeds (YouTube, Twitter, Bluesky) via Markdown shortcuts.
    • Note Management: Includes a sidebar for creating, renaming, switching, and deleting notes.
    • Autosave: Automatically saves content shortly after typing stops.
    • Per-note History: Maintains independent undo/redo history for each note.
    • Adaptive Storage:
      • Tauri (Desktop): Saves notes as JSON files in the application's data directory.
      • Browser (Web): Saves notes in the browser's IndexedDB.
  2. Overview of svelte-lexical

    master
    svelte-lexical is a rich-text editor for Svelte built on top of Lexical, an extensible text editor framework developed by Facebook. The project provides Svelte bindings for the Lexical framework and offers high-level components to simplify editor implementation in Svelte applications.
  3. Overview of svelte-lexical features

    master

    svelte-lexical is a highly configurable rich-text editor designed for Svelte and SvelteKit. It provides a wide range of editing capabilities including:

    • Rich Text: Support for headings, bold, italic, underline, strikethrough, subscript, text color, highlights, lists, and checklists.
    • Customization: Highly configurable with over 25 plugins, allowing for custom themes and custom toolbar designs.
    • Code Blocks: Includes syntax highlighting and Prettier integration for code formatting.
    • Tables: Advanced table manipulation including cell merging and resizing of rows and columns via a contextual menu.
    • Images: Support for inserting images, resizing them, and adding captions.
    • Markdown: Support for Markdown shortcuts and bidirectional conversion (to/from Markdown).
    • Collaboration: Features for real-time collaboration and history management.
  4. Manage reactivity in DecoratorNodes

    master

    When building decorator nodes, you must balance two reactivity systems:

    1. Svelte Reactivity: For UI-level state.
    2. Lexical Reconciler: For the editor's object model.

    Best Practice: Always implement Lexical reconciler reactivity. You must respond to changes in the Lexical node model (such as changes introduced by undo/redo actions) rather than relying solely on Svelte state. For example, a property like showCaption on an image should be driven by the Lexical node state so that undoing a change correctly toggles the UI.

  5. Compare CodeHighlightPrismPlugin and CodeHighlightShikiPlugin

    master

    Choose between the two syntax highlighting plugins based on your requirements for bundle size versus feature depth:

    FeatureCodeHighlightPrismPlugin
    EnginePrism
    Theme SupportLimited (no runtime theme switching)
    Bundle SizeVery small (few KBs)
    Best ForLightweight setups
    FeatureCodeHighlightShikiPlugin
    EngineShiki (TextMate grammars)
    Theme SupportExtensive
    Bundle Size~1.4 MB
    Best ForRicher themes and advanced language support
  6. Use Transformer Groups and Arrays

    master

    Instead of listing every transformer manually, you can use predefined arrays to group common behaviors:

    • ALL_TRANSFORMERS: Includes every supported transformer.
    • TEXT_FORMAT_TRANSFORMERS: Includes text-specific formatting: INLINE_CODE, BOLD_ITALIC_STAR, BOLD_ITALIC_UNDERSCORE, BOLD_STAR, BOLD_UNDERSCORE, HIGHLIGHT, ITALIC_STAR, ITALIC_UNDERSCORE, and STRIKETHROUGH.
    • ELEMENT_TRANSFORMERS: Includes structural elements: HEADING, QUOTE, CODE, UNORDERED_LIST, and ORDERED_LIST.

    You can mix these arrays with individual transformers using the spread operator.

    <!-- Use all transformers -->
    <MarkdownShortcutPlugin transformers={ALL_TRANSFORMERS} />
    
    <!-- Mix an array with specific individual transformers -->
    <MarkdownShortcutPlugin
      transformers={[IMAGE, LINK, ...TEXT_FORMAT_TRANSFORMERS]} 
    />
  7. Understand the two parts of a theme in svelte-lexical

    master

    A theme in svelte-lexical is split into two distinct functional parts:

    1. Editor theme: Defines the visual styles for the actual content inside the editor, such as headings, paragraphs, tables, and lists.
    2. Shell theme: Defines the visual styles for the editor's surrounding UI components, such as the toolbar, dialogs, and menus.

    When building a custom implementation, you must provide both the CSS styles and the mapping of class names used by the editor.

  8. How DecoratorNode works in svelte-lexical

    master

    A DecoratorNode is the primary mechanism for plugging Svelte components into the Lexical editor. It allows Lexical to manage the node's lifecycle while delegating the actual UI rendering to Svelte via a decorator listener.

    Execution Order

    When a decorator node is created or modified, the following sequence occurs:

    1. Reconciler starts
    2. DecoratorNode.createDOM: Called when a node is first created or re-created (e.g., during drag-and-drop). It creates the target DOM element where the Svelte component will live.
    3. DecoratorNode.decorate: Called whenever node properties change. It returns the component and its props (or a function to update props in Svelte 5 using $state runes).
    4. Reconciler ends
    5. Mutation Listeners: Called to signal if a node was created, updated, or destroyed.
    6. Decorator Listener: Registered via registerDecoratorListener. This is where the actual Svelte rendering takes place. Note that the listener receives the full list of decorator nodes in the document, not just the changed ones.

    Key Constraints

    • Ephemeral Objects: Decorator nodes (like ImageNode) are frequently cloned and replaced to maintain the undo/redo stack. Do not hold direct references to a Decorator Node object. Instead, store the nodeKey and retrieve the node using editor.getElementByKey(nodeKey) when needed.
    • Data Storage: Nodes should only hold data within their own properties. Do not attempt to store external references inside the node instance.
    /* Conceptual execution flow summary */
    // 1. Reconciler starts
    // 2. createDOM()
    // 3. decorate()
    // 4. Reconciler ends
    // 5. Mutation Listeners (created, updated, destroyed)
    // 6. Decorator Listener (renders via Svelte)
  9. Qalam Project Structure

    master

    Understanding the internal organization of the Qalam demo:

    • src/App.svelte: Top-level layout containing the sidebar and editor.
    • src/lib/Sidebar.svelte: Handles the notes list and create/rename/delete operations.
    • src/lib/Editor.svelte: Manages the svelte-lexical editor setup, autosave logic, and per-note history.
    • src/lib/Toolbar.svelte: Provides the formatting toolbar.
    • src/lib/Settings.svelte: Displays the storage location for notes.
    • src/lib/notesStore.svelte.ts: A reactive store coordinating notes state with the backend.
    • src/lib/notesBackend.ts: The storage abstraction that switches between the Tauri filesystem and IndexedDB.
    • src-tauri/: Contains the Tauri desktop configuration and Rust backend code.