Code Monkey home page Code Monkey logo

blocksuite's Introduction

BlockSuite

BlockSuite logo and name

Codecov Checks Status Issues Closed NPM Latest Release NPM Nightly Release Open in CodeSandbox Open in StackBlitz Join Discord


BlockSuite (pronounced "block sweet") is the open-source editor project behind AFFiNE. It provides an out-of-the-box block-based editor built on top of a framework designed for general-purpose collaborative applications. This monorepo maintains both the editor and the underlying framework.

โš ๏ธ This project is under heavy development and is in a stage of rapid evolution. Stay tuned or see our roadmap here!

Introduction

BlockSuite works very differently than traditional rich text frameworks:

  • For the data model, BlockSuite eliminates the need to work with data-driven DSLs (e.g., operations, actions, commands, transforms). Instead, it utilizes CRDT as the single source of truth, offering a strongly-typed block tree model built on Yjs. With BlockSuite, manipulating blocks becomes as simple as updating a todo list. Moreover, by fully harnessing the power of CRDT, it supports zero-cost time travel, real-time collaboration, and out-of-the-box pluggable persistence backends.
  • For rich text editing, BlockSuite seamlessly organizes rich text content into discrete blocks. In BlockSuite, a document with 100 paragraphs can be rendered into 100 text blocks or 100 individual rich text editor instances, effectively eliminating the outdated practice of consolidating all content into a single, risky contenteditable monolith.
  • At the rendering layer, BlockSuite remains framework agnostic. It doesn't limit the block tree rendering to the DOM. Not only does it implement its entire document editing UI using Web Components, but it also offers a hybrid canvas-based renderer for whiteboard content sections. Both renderers can coexist on the same page and share a unified centralized data store.

BlockSuite is not intended to be yet another plugin-based rich text editing framework. Instead, it encourages building various collaborative applications directly through whatever UI framework you're comfortable with. To this end, we will try to open-source more foundational modules as reusable packages for this in the BlockSuite project.

Although BlockSuite is still in its early stages, you can already use the @blocksuite/editor package, the collaborative editor used in AFFiNE Alpha. Note that this editor is also a web component and is completely framework-independent!

Resources

Getting Started

The @blocksuite/editor package contains the editor built into AFFiNE. Its nightly versions are released daily based on the master branch, and they are always tested on CI. This means that the nightly versions can already be used in real-world projects like AFFiNE at any time:

pnpm i @blocksuite/editor@nightly

If you want to easily reuse most of the rich-text editing features, you can use the SimpleAffineEditor web component directly (code example here):

import { SimpleAffineEditor } from '@blocksuite/editor';
import '@blocksuite/editor/themes/affine.css';

const editor = new SimpleAffineEditor();
document.body.appendChild(editor);

Or equivalently, you can also use the declarative style:

<body>
  <simple-affine-editor></simple-affine-editor>
  <script type="module">
    import '@blocksuite/editor';
    import '@blocksuite/editor/themes/affine.css';
  </script>
</body>

๐Ÿ‘‰ Try SimpleAffineEditor online

However, the SimpleAffineEditor here is just a thin wrapper with dozens of lines that doesn't enable the opt-in collaboration and data persistence features. If you are going to support more complicated real-world use cases (e.g., with customized block models and configured data sources), this will involve the use of these three following core packages:

  • The packages/store package is a data store built for general-purpose state management.
  • The packages/blocks package holds the default BlockSuite editable blocks.
  • The packages/editor package ships a complete BlockSuite-based editor.
pnpm i \
  @blocksuite/store@nightly \
  @blocksuite/blocks@nightly \
  @blocksuite/editor@nightly

And here is a minimal collaboration-ready editor showing how these underlying BlockSuite packages are composed together:

๐Ÿšง Here we will work with the concepts of Workspace, Page, Block and Slot. These are the primitives for building a block-based collaborative application. We are preparing a comprehensive documentation about their usage!

import '@blocksuite/blocks';
import { Workspace, Page } from '@blocksuite/store';
import { AffineSchemas } from '@blocksuite/blocks/models';
import { EditorContainer } from '@blocksuite/editor';

function main() {
  // Create a workspace with one default page
  const workspace = new Workspace({ id: 'test' }).register(AffineSchemas);
  const page = workspace.createPage('page0');

  // Create default blocks in the page
  const pageBlockId = page.addBlock('affine:page');
  const frameId = page.addBlock('affine:frame', {}, pageBlockId);
  page.addBlock('affine:paragraph', {}, frameId);

  // Init editor with the page store
  const editor = new EditorContainer();
  editor.page = page;
  document.body.appendChild(editor);
}

main();

For React developers, check out the @blocksuite/react doc for React components and hooks support.

Current Status (@blocksuite/editor)

For more detailed planning and progress, please checkout our GitHub project.

  • Basic text editing
    • โœ… Paragraph with inline style
    • โœ… Nested list
    • โœ… Code block
    • โœ… Markdown shortcuts
  • Block-level editing
    • โœ… Inline text format bar
    • โœ… Inline slash menu
    • โœ… Block hub
    • โœ… Block drag handle
    • โœ… Block-level selection
  • Rich-content
    • โœ… Image block
    • ๐Ÿšง Database block
    • ๐Ÿ“Œ Third-party embedded block
  • Whiteboard (edgeless mode)
    • โœ… Zooming and panning
    • โœ… Frame block
    • โš›๏ธ Shape element
    • โš›๏ธ Handwriting element
    • ๐Ÿšง Shape connector
    • ๐Ÿšง Grouping
  • Playground
    • โœ… Multiplayer collaboration
    • โœ… Local data persistence
    • โœ… E2E test suite
  • Developer experience
    • โœ… Block tree update API
    • โœ… Zero cost time travel (undo/redo)
    • โœ… Reusable NPM package
    • โœ… React hooks integration
    • ๐Ÿšง Dynamic component registration
    • ๐Ÿ“Œ Dynamic block registration

Icons above correspond to the following meanings:

  • โœ… - Beta
  • โš›๏ธ - Alpha
  • ๐Ÿšง - Developing
  • ๐Ÿ“Œ - Planned

Building

See BUILDING.md for instructions on how to build BlockSuite from source code.

Contributing

BlockSuite accepts pull requests on GitHub. Before you start contributing, please make sure you have read and accepted our Contributor License Agreement. To indicate your agreement, simply edit this file and submit a pull request.

License

MPL 2.0

blocksuite's People

Contributors

akarachen avatar alt1o avatar colelawrence avatar darkskygit avatar devlzl avatar diamondthree avatar donaldxdonald avatar doodlewind avatar fi3ework avatar flrande avatar fundon avatar github-actions[bot] avatar hanchayi avatar himself65 avatar innei avatar jimmfly avatar lawvs avatar lucinyan avatar pengx17 avatar perfectpan avatar pionxzh avatar qinluhe avatar qishaoxuan avatar saikasakura avatar saul-mirone avatar shortcipher5 avatar thorseraq avatar tzhangchi avatar zqran avatar zuoxiaodong0815 avatar

Stargazers

 avatar

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.