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

feat: add controls provider to render controls outside slate context #5459

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

z4o4z
Copy link

@z4o4z z4o4z commented Jun 21, 2023

Description
This pull request adds a new ToolbarProvider component to render controls/toolbar outside the slate context.

Example

<>
      <ToolbarProvider editor={editor}>
        <Toolbar style={{ marginBottom: 20 }}>
          <MarkButton format="bold" icon="format_bold" />
          <MarkButton format="italic" icon="format_italic" />
          <MarkButton format="underline" icon="format_underlined" />
          <MarkButton format="code" icon="code" />
          <BlockButton format="heading-one" icon="looks_one" />
          <BlockButton format="heading-two" icon="looks_two" />
          <BlockButton format="block-quote" icon="format_quote" />
          <BlockButton format="numbered-list" icon="format_list_numbered" />
          <BlockButton format="bulleted-list" icon="format_list_bulleted" />
          <BlockButton format="left" icon="format_align_left" />
          <BlockButton format="center" icon="format_align_center" />
          <BlockButton format="right" icon="format_align_right" />
          <BlockButton format="justify" icon="format_align_justify" />
        </Toolbar>
      </ControlsProvider>

      <Slate editor={editor} initialValue={initialValue}>
        <Editable
          renderElement={renderElement}
          renderLeaf={renderLeaf}
          placeholder="Enter some rich text…"
          spellCheck
          autoFocus
          onKeyDown={event => {
            for (const hotkey in HOTKEYS) {
              if (isHotkey(hotkey, event as any)) {
                event.preventDefault()
                const mark = HOTKEYS[hotkey]
                toggleMark(editor, mark)
              }
            }
          }}
        />
      </Slate>
    </>

Context

  • Moved most of the slate component logic into a provider component to reuse it in the toolbar-provider.
  • toolbar-provider is a simpler version of slate, it doesn't mutate the editor since it must be mutated in the slate component only.
  • changed EDITOR_TO_ON_CHANGE map to be a map of sets, this way we can have multiple listeners for the same editor

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot
Copy link

changeset-bot bot commented Jun 21, 2023

🦋 Changeset detected

Latest commit: 49a0373

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator

@dylans dylans left a comment

Choose a reason for hiding this comment

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

I'm not opposed to this change, but I've been sitting on it a bit as it requires changing a few APIs to support it and I'm not yet sure what consequences that might introduce to others.

@z4o4z
Copy link
Author

z4o4z commented Jun 29, 2023

hm, what kind of APIs does the PR change? The only thing I can see is EDITOR_TO_ON_CHANGE, but it's internal and used in only one place, so shouldn't be any problems, isn't it?

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.

None yet

2 participants