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

Autoformat should not take effect within code blocks #3814

Open
tomdyqin opened this issue Nov 27, 2024 · 3 comments
Open

Autoformat should not take effect within code blocks #3814

tomdyqin opened this issue Nov 27, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@tomdyqin
Copy link
Contributor

Description

Autoformat should not take effect within code blocks

Reproduction URL

No response

Reproduction steps

1. Go to 'playground'
2. Click on ' '
3. Input '# '
4. See '#' will remove, becase the markdown autoformat is take effect

Plate version

39.2.15

Slate React version

0.110.3

Screenshots

No response

Logs

No response

Browsers

No response

@tomdyqin tomdyqin added the bug Something isn't working label Nov 27, 2024
@tomdyqin
Copy link
Contributor Author

tomdyqin commented Nov 27, 2024

  // plate/packages/autoformat/src/lib/withAutoformat.ts
  editor.insertText = (text) => {
    if (!isCollapsed(editor.selection)) return insertText(text);

    if (getCodeLineEntry(editor)) return insertText(text); // I'm not sure if adding this processing here is too much

    for (const rule of getOptions().rules!) {
      const { insertTrigger, mode = 'text', query } = rule;

      if (query && !query(editor as any, { ...rule, text })) continue;

      const autoformatter: Record<typeof mode, any> = {
        block: autoformatBlock,
        mark: autoformatMark,
        text: autoformatText,
      };

      if (
        autoformatter[mode]?.(editor, {
          ...(rule as any),
          text,
        })
      ) {
        return insertTrigger && insertText(text);
      }
    }

    insertText(text);
  };

@yf-yang
Copy link
Collaborator

yf-yang commented Dec 6, 2024

I think the adequate solution is adding a "query" function inside the rules. You already see the code:

if (query && !query(editor as any, { ...rule, text })) continue;

Would you like to submit a PR?

@yf-yang
Copy link
Collaborator

yf-yang commented Dec 6, 2024

Almost every rule needs such a query (which means disabling autoformat when selection is within a code block). Although it is a little bit awkward to add the query to each rule, there is no better way to do so for now.

To further reuse codes, it may relate to a larger topic:
Some of the "container" elements (blocks) creates a brand new "context", for example, code block disallows autoformat and may other marks; columns, tables should support drag and drop...

Is it necessary to provide such a mechanism that decouples certain "context" and plugin configurations? I am not quite sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants