File tree Expand file tree Collapse file tree
packages/super-editor/src/extensions/field-annotation Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -448,6 +448,39 @@ export const FieldAnnotation = Node.create({
448448 commands . addFieldAnnotation ( from , attrs , editorFocus ) ;
449449 } ,
450450
451+ /**
452+ * Replace field annotation.
453+ * @param fieldsArray array of fields with attrs to add as annotation.
454+ * @example
455+ * editor.commands.replaceWithFieldAnnotation([
456+ * from: 20,
457+ * to: 45,
458+ * attrs: {
459+ * fieldType: 'TEXTINPUT'
460+ * fieldColor: '#980043'
461+ * }
462+ * ])
463+ */
464+ replaceWithFieldAnnotation :
465+ ( fieldsArray ) =>
466+ ( { editor, dispatch, state, tr} ) => {
467+ if ( ! dispatch ) return true ;
468+ tr . setMeta ( 'fieldAnnotationReplace' , true ) ;
469+
470+ fieldsArray . forEach ( ( annotation ) => {
471+ let { from, to, attrs} = annotation ;
472+ let { schema} = editor ;
473+
474+ let newPosFrom = tr . mapping . map ( from ) ;
475+ let newPosTo = tr . mapping . map ( to ) ;
476+ let node = schema . nodes [ this . name ] . create ( { ...attrs } , null , null ) ;
477+
478+ tr . replaceWith ( newPosFrom , newPosTo , node ) ;
479+ } ) ;
480+
481+ return true ;
482+ } ,
483+
451484 /**
452485 * Update annotations associated with a field.
453486 * @param fieldIdOrArray The field ID or array of field IDs.
You can’t perform that action at this time.
0 commit comments