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

The toggleNodeType method should have a matching mechanism, otherwise it may cause the nodeType of the element to be incorrectly changed #3024

Open
tomdyqin opened this issue Mar 14, 2024 · 0 comments

Comments

@tomdyqin
Copy link
Contributor

tomdyqin commented Mar 14, 2024

1、The toggleNodeType method should have a matching mechanism, otherwise it may cause the nodeType of the element to be incorrectly changed。

the test example

const input = (
  <editor>
    <p>test</p>
     <img />
  </editor>
) as any as PlateEditor;

const output = (
  <editor>
    <h1>test</h1>
     <img src="test"/>
  </editor>
) as any;

const errorOutput = (
  <editor>
    <h1>test</h1>
     <h1 src="test"/>
  </editor>
) as any;

it('should be', () => {
  toggleNodeType(input, { activeType: 'h1' });

  expect(input.children).toEqual(output.children);
});

2、Why block changes here?

 if (isActive && activeType === inactiveType) return;

the test example

const input = (
  <editor>
    <p>test</p>
     <h1>test h1</h1>
  </editor>
) as any as PlateEditor;

const output = (
  <editor>
    <h1>test</h1>
    <h1>test h1</h1>
  </editor>
) as any;

const errorOutput = (
  <editor>
    <p>test</p>
     <h1>test h1</h1>
  </editor>
) as any;

it('should be', () => {
  toggleNodeType(input, { activeType: 'h1' });

  expect(input.children).toEqual(output.children);
});

This is the toggleNodeType method I am currently using

const toggleNodeType = <V extends Value>(
  editor: PlateEditor<V>,
  options: ToggleNodeTypeOptions,
  editorNodesOptions?: GetNodeEntriesOptions<V>
) => {
  const { activeType, inactiveType = getPluginType(editor, ELEMENT_DEFAULT) } = options;
  if (!activeType || !editor.selection) return;

  const isActive = someNode(editor, {
    ...editorNodesOptions,
    match: {
      type: activeType,
    },
  });

  setElements<Value>(editor, {
    type: isActive ? inactiveType : activeType,
  }, {
    match: (n, p) => (editorNodesOptions?.match ? editorNodesOptions?.match(n, p) :  isBlock(editor, n)),
  });
};

// when i want to toggle nodeType to h1
 const validTypes = [
    ELEMENT_PARAGRAPH,
    ELEMENT_H1,
    ELEMENT_H2,
    ELEMENT_H3,
    ELEMENT_H4,
    ELEMENT_TODO_LI,
  ];
  toggleNodeType(editor, options, {
    match: n => validTypes.includes(n.type as string),
  });

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar
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

No branches or pull requests

1 participant