Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,39 @@ export const FieldAnnotation = Node.create({
commands.addFieldAnnotation(from, attrs, editorFocus);
},

/**
* Replace field annotation.
Copy link
Copy Markdown
Contributor

@artem-harbour artem-harbour May 29, 2025

Choose a reason for hiding this comment

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

Replace positions with field annotations.

+@param description.

* @param fieldsArray array of fields with attrs to add as annotation.
* @example
* editor.commands.replaceWithFieldAnnotation([
* from: 20,
* to: 45,
* attrs: {
* fieldType: 'TEXTINPUT'
* fieldColor: '#980043'
* }
* ])
*/
replaceWithFieldAnnotation:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the function signature could be improved here, it's very much tied to the field name now. Although these don't necessarily have to be fields, just positions.

Example

{
  replaceWithFieldAnnotation:
    (positions) =>
    ({ editor, dispatch, state, tr }) => {
      if (!dispatch) return true;

      tr.setMeta('fieldAnnotationReplace', true);

      positions.forEach((position) => {
        let { from, to, attrs } = position;
        let { schema } = editor;

        let newPosFrom = tr.mapping.map(from);
        let newPosTo = tr.mapping.map(to);
        let node = schema.nodes[this.name].create({...attrs}, null, null);

        tr.replaceWith(newPosFrom, newPosTo, node);
      });

      return true;
  },
}

(fieldsArray) =>
({editor, dispatch, state, tr}) => {
if (!dispatch) return true;
tr.setMeta('fieldAnnotationReplace', true);

fieldsArray.forEach((annotation) => {
let {from, to, attrs} = annotation;
let {schema} = editor;

let newPosFrom = tr.mapping.map(from);
let newPosTo = tr.mapping.map(to);
let node = schema.nodes[this.name].create({...attrs}, null, null);

tr.replaceWith(newPosFrom, newPosTo, node);
});

return true;
},

/**
* Update annotations associated with a field.
* @param fieldIdOrArray The field ID or array of field IDs.
Expand Down
Loading