chore(ckeditor5-footnotes): fix references: Element -> ModelElement

This commit is contained in:
Elian Doran 2025-07-12 19:20:23 +03:00
parent 6ead31b45f
commit a901e92573
No known key found for this signature in database
5 changed files with 28 additions and 28 deletions

View File

@ -1,4 +1,4 @@
import { type Editor, ModelText, ModelTextProxy, type Element, type Range, type Autoformat, inlineAutoformatEditing } from 'ckeditor5'; import { type Editor, ModelText, ModelTextProxy, type ModelElement, type Range, type Autoformat, inlineAutoformatEditing } from 'ckeditor5';
import { COMMANDS, ELEMENTS } from '../constants.js'; import { COMMANDS, ELEMENTS } from '../constants.js';
import { modelQueryElement, modelQueryElementsAll } from '../utils.js'; import { modelQueryElement, modelQueryElementsAll } from '../utils.js';
@ -68,7 +68,7 @@ const regexMatchCallback = (
* Footnotes only get inserted if the matching range is an integer between 1 * Footnotes only get inserted if the matching range is an integer between 1
* and the number of existing footnotes + 1. * and the number of existing footnotes + 1.
*/ */
const formatCallback = ( ranges: Array<Range>, editor: Editor, rootElement: Element ): boolean | undefined => { const formatCallback = ( ranges: Array<Range>, editor: Editor, rootElement: ModelElement ): boolean | undefined => {
const command = editor.commands.get( COMMANDS.insertFootnote ); const command = editor.commands.get( COMMANDS.insertFootnote );
if ( !command || !command.isEnabled ) { if ( !command || !command.isEnabled ) {
return; return;
@ -108,7 +108,7 @@ const formatCallback = ( ranges: Array<Range>, editor: Editor, rootElement: Elem
/** /**
* Adds functionality to support creating footnotes using markdown syntax, e.g. `[^1]`. * Adds functionality to support creating footnotes using markdown syntax, e.g. `[^1]`.
*/ */
export const addFootnoteAutoformatting = ( editor: Editor, rootElement: Element ): void => { export const addFootnoteAutoformatting = ( editor: Editor, rootElement: ModelElement ): void => {
if ( editor.plugins.has( 'Autoformat' ) ) { if ( editor.plugins.has( 'Autoformat' ) ) {
const autoformatPluginInstance = editor.plugins.get( 'Autoformat' ) as Autoformat; const autoformatPluginInstance = editor.plugins.get( 'Autoformat' ) as Autoformat;
inlineAutoformatEditing( inlineAutoformatEditing(

View File

@ -1,4 +1,4 @@
import { type Editor, type DowncastConversionApi, type ViewContainerElement, Element, toWidget, toWidgetEditable } from 'ckeditor5'; import { type Editor, type DowncastConversionApi, type ViewContainerElement, ModelElement, toWidget, toWidgetEditable } from 'ckeditor5';
import { ATTRIBUTES, CLASSES, ELEMENTS } from '../constants.js'; import { ATTRIBUTES, CLASSES, ELEMENTS } from '../constants.js';
import { viewQueryElement } from '../utils.js'; import { viewQueryElement } from '../utils.js';
@ -232,7 +232,7 @@ export const defineConverters = ( editor: Editor ): void => {
* for both data and editing downcasts. * for both data and editing downcasts.
*/ */
function createFootnoteBackLinkViewElement( function createFootnoteBackLinkViewElement(
modelElement: Element, modelElement: ModelElement,
conversionApi: DowncastConversionApi conversionApi: DowncastConversionApi
): ViewContainerElement { ): ViewContainerElement {
const viewWriter = conversionApi.writer; const viewWriter = conversionApi.writer;
@ -264,7 +264,7 @@ function createFootnoteBackLinkViewElement(
* data downcast and editing downcast conversions. * data downcast and editing downcast conversions.
*/ */
function createFootnoteReferenceViewElement( function createFootnoteReferenceViewElement(
modelElement: Element, modelElement: ModelElement,
conversionApi: DowncastConversionApi conversionApi: DowncastConversionApi
): ViewContainerElement { ): ViewContainerElement {
const viewWriter = conversionApi.writer; const viewWriter = conversionApi.writer;
@ -301,7 +301,7 @@ function createFootnoteReferenceViewElement(
* data downcast and editing downcast conversions. * data downcast and editing downcast conversions.
*/ */
function createFootnoteItemViewElement( function createFootnoteItemViewElement(
modelElement: Element, modelElement: ModelElement,
conversionApi: DowncastConversionApi conversionApi: DowncastConversionApi
): ViewContainerElement { ): ViewContainerElement {
const viewWriter = conversionApi.writer; const viewWriter = conversionApi.writer;
@ -330,7 +330,7 @@ function createFootnoteItemViewElement(
*/ */
function updateFootnoteReferenceView( function updateFootnoteReferenceView(
data: { data: {
item: Element; item: ModelElement;
attributeOldValue: string; attributeOldValue: string;
attributeNewValue: string; attributeNewValue: string;
}, },
@ -339,7 +339,7 @@ function updateFootnoteReferenceView(
) { ) {
const { item, attributeNewValue: newIndex } = data; const { item, attributeNewValue: newIndex } = data;
if ( if (
!( item instanceof Element ) || !( item instanceof ModelElement ) ||
!conversionApi.consumable.consume( item, `attribute:${ ATTRIBUTES.footnoteIndex }:${ ELEMENTS.footnoteReference }` ) !conversionApi.consumable.consume( item, `attribute:${ ATTRIBUTES.footnoteIndex }:${ ELEMENTS.footnoteReference }` )
) { ) {
return; return;

View File

@ -11,7 +11,7 @@ import { defineSchema } from './schema.js';
import { ATTRIBUTES, COMMANDS, ELEMENTS } from '../constants.js'; import { ATTRIBUTES, COMMANDS, ELEMENTS } from '../constants.js';
import InsertFootnoteCommand from '../insert-footnote-command.js'; import InsertFootnoteCommand from '../insert-footnote-command.js';
import { modelQueryElement, modelQueryElementsAll } from '../utils.js'; import { modelQueryElement, modelQueryElementsAll } from '../utils.js';
import { Autoformat, Batch, Element, Plugin, ModelRootElement, viewToModelPositionOutsideModelElement, Widget, Writer } from 'ckeditor5'; import { Autoformat, Batch, ModelElement, Plugin, ModelRootElement, viewToModelPositionOutsideModelElement, Widget, Writer } from 'ckeditor5';
export default class FootnoteEditing extends Plugin { export default class FootnoteEditing extends Plugin {
@ -56,7 +56,7 @@ export default class FootnoteEditing extends Plugin {
if ( diffItem.type === 'attribute' && diffItem.attributeKey === ATTRIBUTES.footnoteIndex ) { if ( diffItem.type === 'attribute' && diffItem.attributeKey === ATTRIBUTES.footnoteIndex ) {
const { attributeNewValue: newFootnoteIndex } = diffItem; const { attributeNewValue: newFootnoteIndex } = diffItem;
const footnote = [ ...diffItem.range.getItems() ].find( item => item.is( 'element', ELEMENTS.footnoteItem ) ); const footnote = [ ...diffItem.range.getItems() ].find( item => item.is( 'element', ELEMENTS.footnoteItem ) );
const footnoteId = footnote instanceof Element && footnote.getAttribute( ATTRIBUTES.footnoteId ); const footnoteId = footnote instanceof ModelElement && footnote.getAttribute( ATTRIBUTES.footnoteId );
if ( !footnoteId ) { if ( !footnoteId ) {
return; return;
} }
@ -143,7 +143,7 @@ export default class FootnoteEditing extends Plugin {
* batch these changes with the ones that instantiated them, * batch these changes with the ones that instantiated them,
* such that the set can be undone with a single action. * such that the set can be undone with a single action.
*/ */
private _clearContents( modelWriter: Writer, footnoteContent: Element ) { private _clearContents( modelWriter: Writer, footnoteContent: ModelElement ) {
const contents = modelWriter.createRangeIn( footnoteContent ); const contents = modelWriter.createRangeIn( footnoteContent );
modelWriter.appendElement( 'paragraph', footnoteContent ); modelWriter.appendElement( 'paragraph', footnoteContent );
modelWriter.remove( contents ); modelWriter.remove( contents );
@ -155,7 +155,7 @@ export default class FootnoteEditing extends Plugin {
* which triggers the `updateReferenceIds` method. modelWriter is passed in to batch these changes with * which triggers the `updateReferenceIds` method. modelWriter is passed in to batch these changes with
* the ones that instantiated them, such that the set can be undone with a single action. * the ones that instantiated them, such that the set can be undone with a single action.
*/ */
private _removeFootnote( modelWriter: Writer, footnote: Element ) { private _removeFootnote( modelWriter: Writer, footnote: ModelElement ) {
// delete the current footnote and its references, // delete the current footnote and its references,
// and renumber subsequent footnotes. // and renumber subsequent footnotes.
if ( !this.editor ) { if ( !this.editor ) {
@ -185,7 +185,7 @@ export default class FootnoteEditing extends Plugin {
// immediately deletes the footnote. This deliberately sets the new selection position // immediately deletes the footnote. This deliberately sets the new selection position
// to avoid that. // to avoid that.
const neighborFootnote = index === 0 ? footnoteSection.getChild( index ) : footnoteSection.getChild( ( index ?? 0 ) - 1 ); const neighborFootnote = index === 0 ? footnoteSection.getChild( index ) : footnoteSection.getChild( ( index ?? 0 ) - 1 );
if ( !( neighborFootnote instanceof Element ) ) { if ( !( neighborFootnote instanceof ModelElement ) ) {
return; return;
} }

View File

@ -1,4 +1,4 @@
import { Command, type Element, type ModelRootElement, type Writer } from "ckeditor5"; import { Command, type ModelElement, type ModelRootElement, type Writer } from "ckeditor5";
import { ATTRIBUTES, ELEMENTS } from './constants.js'; import { ATTRIBUTES, ELEMENTS } from './constants.js';
import { modelQueryElement } from './utils.js'; import { modelQueryElement } from './utils.js';
@ -82,7 +82,7 @@ export default class InsertFootnoteCommand extends Command {
/** /**
* Returns the footnote section if it exists, or creates on if it doesn't. * Returns the footnote section if it exists, or creates on if it doesn't.
*/ */
private _getFootnoteSection( writer: Writer, rootElement: ModelRootElement ): Element { private _getFootnoteSection( writer: Writer, rootElement: ModelRootElement ): ModelElement {
const footnoteSection = modelQueryElement( this.editor, rootElement, element => const footnoteSection = modelQueryElement( this.editor, rootElement, element =>
element.is( 'element', ELEMENTS.footnoteSection ) element.is( 'element', ELEMENTS.footnoteSection )
); );

View File

@ -1,4 +1,4 @@
import { type Editor, Element, ModelText, ModelTextProxy, ViewElement } from 'ckeditor5'; import { type Editor, ModelElement, ModelText, ModelTextProxy, ViewElement } from 'ckeditor5';
// There's ample DRY violation in this file; type checking // There's ample DRY violation in this file; type checking
// polymorphism without full typescript is just incredibly finicky. // polymorphism without full typescript is just incredibly finicky.
@ -11,14 +11,14 @@ import { type Editor, Element, ModelText, ModelTextProxy, ViewElement } from 'ck
*/ */
export const modelQueryElementsAll = ( export const modelQueryElementsAll = (
editor: Editor, editor: Editor,
rootElement: Element, rootElement: ModelElement,
predicate: ( item: Element ) => boolean = _ => true predicate: ( item: ModelElement ) => boolean = _ => true
): Array<Element> => { ): Array<ModelElement> => {
const range = editor.model.createRangeIn( rootElement ); const range = editor.model.createRangeIn( rootElement );
const output: Array<Element> = []; const output: Array<ModelElement> = [];
for ( const item of range.getItems() ) { for ( const item of range.getItems() ) {
if ( !( item instanceof Element ) ) { if ( !( item instanceof ModelElement ) ) {
continue; continue;
} }
@ -35,7 +35,7 @@ export const modelQueryElementsAll = (
*/ */
export const modelQueryTextAll = ( export const modelQueryTextAll = (
editor: Editor, editor: Editor,
rootElement: Element, rootElement: ModelElement,
predicate: ( item: ModelText | ModelTextProxy ) => boolean = _ => true predicate: ( item: ModelText | ModelTextProxy ) => boolean = _ => true
): Array<ModelText | ModelTextProxy> => { ): Array<ModelText | ModelTextProxy> => {
const range = editor.model.createRangeIn( rootElement ); const range = editor.model.createRangeIn( rootElement );
@ -59,13 +59,13 @@ export const modelQueryTextAll = (
*/ */
export const modelQueryElement = ( export const modelQueryElement = (
editor: Editor, editor: Editor,
rootElement: Element, rootElement: ModelElement,
predicate: ( item: Element ) => boolean = _ => true predicate: ( item: ModelElement ) => boolean = _ => true
): Element | null => { ): ModelElement | null => {
const range = editor.model.createRangeIn( rootElement ); const range = editor.model.createRangeIn( rootElement );
for ( const item of range.getItems() ) { for ( const item of range.getItems() ) {
if ( !( item instanceof Element ) ) { if ( !( item instanceof ModelElement ) ) {
continue; continue;
} }
@ -82,7 +82,7 @@ export const modelQueryElement = (
*/ */
export const modelQueryText = ( export const modelQueryText = (
editor: Editor, editor: Editor,
rootElement: Element, rootElement: ModelElement,
predicate: ( item: ModelText | ModelTextProxy ) => boolean = _ => true predicate: ( item: ModelText | ModelTextProxy ) => boolean = _ => true
): ModelText | ModelTextProxy | null => { ): ModelText | ModelTextProxy | null => {
const range = editor.model.createRangeIn( rootElement ); const range = editor.model.createRangeIn( rootElement );