From 633969300b0b13b3d80eaef8967326535ff1a138 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Mon, 26 Aug 2024 11:14:33 +0200 Subject: [PATCH] [refactor] Update some usages to new refs interface Change-Id: Idfffbe92ad1385e85b078bc4794a529c5f770eae --- modules/ve-cite/ve.ui.MWReferenceEditPanel.js | 25 ++++++++++--------- .../ve-cite/ve.ui.MWReferenceSearchWidget.js | 2 +- .../ve.ui.MWReferenceEditPanel.test.js | 12 +++------ .../ve.ui.MWReferenceSearchWidget.test.js | 3 ++- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/modules/ve-cite/ve.ui.MWReferenceEditPanel.js b/modules/ve-cite/ve.ui.MWReferenceEditPanel.js index 339d3c8a4..62bd92796 100644 --- a/modules/ve-cite/ve.ui.MWReferenceEditPanel.js +++ b/modules/ve-cite/ve.ui.MWReferenceEditPanel.js @@ -246,15 +246,13 @@ ve.ui.MWReferenceEditPanel.prototype.setFormFieldsFromRef = function ( ref ) { * @param {ve.dm.MWReferenceModel} ref */ ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref ) { - const group = this.internalList.getNodeGroup( ref.getListGroup() ); - const nodes = ve.getProp( group, 'keyedNodes', ref.getListKey() ); - const usages = nodes ? nodes.filter( - ( node ) => !node.findParent( ve.dm.MWReferencesListNode ) - ).length : 0; - + const usageCount = ve.dm.MWDocumentReferences.static.refsForDoc( this.internalList.getDocument() ) + .getGroupRefs( ref.getListGroup() ) + .getRefUsages( ref.getListKey() ) + .length; this.reuseWarning - .toggle( usages > 1 ) - .setLabel( mw.msg( 'cite-ve-dialog-reference-editing-reused-long', usages ) ); + .toggle( usageCount > 1 ) + .setLabel( mw.msg( 'cite-ve-dialog-reference-editing-reused-long', usageCount ) ); }; /** @@ -263,15 +261,18 @@ ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref */ ve.ui.MWReferenceEditPanel.prototype.updateExtendsWarningFromRef = function ( ref ) { if ( ref.extendsRef ) { - const mainRefId = this.internalList.keys.indexOf( ref.extendsRef ); + const parentNode = ve.dm.MWDocumentReferences.static.refsForDoc( this.internalList.getDocument() ) + .getGroupRefs( ref.getListGroup() ) + .getInternalModelNode( ref.extendsRef ); this.extendsWarning.setLabel( $( '

' ) .text( mw.msg( 'cite-ve-dialog-reference-editing-extends' ) ) - .append( mainRefId === -1 ? + .append( parentNode ? + new ve.ui.MWPreviewElement( parentNode, { useView: true } ).$element : $( '

' ) .addClass( 've-ui-mwReferenceContextItem-muted' ) - .text( ve.msg( 'cite-ve-dialog-reference-missing-parent-ref' ) ) : - new ve.ui.MWPreviewElement( this.internalList.getItemNode( mainRefId ), { useView: true } ).$element ) + .text( ve.msg( 'cite-ve-dialog-reference-missing-parent-ref' ) ) + ) ); this.extendsWarning.toggle( true ); } else { diff --git a/modules/ve-cite/ve.ui.MWReferenceSearchWidget.js b/modules/ve-cite/ve.ui.MWReferenceSearchWidget.js index 600d12425..6199840d2 100644 --- a/modules/ve-cite/ve.ui.MWReferenceSearchWidget.js +++ b/modules/ve-cite/ve.ui.MWReferenceSearchWidget.js @@ -172,7 +172,7 @@ ve.ui.MWReferenceSearchWidget.prototype.buildSearchIndex = function () { let $element; // Make visible text, citation and reference name searchable let text = ( citation + ' ' + name ).toLowerCase(); - const itemNode = this.internalList.getItemNode( node.getAttribute( 'listIndex' ) ); + const itemNode = groupRefs.getInternalModelNode( listKey ); if ( itemNode.length ) { $element = new ve.ui.MWPreviewElement( itemNode, { useView: true } ).$element; text = $element.text().toLowerCase() + ' ' + text; diff --git a/tests/qunit/ve-cite/ve.ui.MWReferenceEditPanel.test.js b/tests/qunit/ve-cite/ve.ui.MWReferenceEditPanel.test.js index 2291c3a66..77980d233 100644 --- a/tests/qunit/ve-cite/ve.ui.MWReferenceEditPanel.test.js +++ b/tests/qunit/ve-cite/ve.ui.MWReferenceEditPanel.test.js @@ -6,15 +6,9 @@ QUnit.test( 'setReferenceForEditing', ( assert ) => { ve.init.target.surface = { commandRegistry: { registry: {} } }; const editPanel = new ve.ui.MWReferenceEditPanel(); - const emptyDoc = new ve.dm.Document( [ - { type: 'paragraph', internal: { generated: 'empty' } }, - { type: '/paragraph' }, - { type: 'internalList' }, - { type: '/internalList' } - ] ); - const ref = new ve.dm.MWReferenceModel( emptyDoc ); - - editPanel.setInternalList( emptyDoc.getInternalList() ); + const doc = ve.dm.citeExample.createExampleDocument( 'references' ); + const ref = new ve.dm.MWReferenceModel( doc ); + editPanel.setInternalList( doc.getInternalList() ); const changeHandlerStub = sinon.stub(); editPanel.connect( changeHandlerStub ); diff --git a/tests/qunit/ve-cite/ve.ui.MWReferenceSearchWidget.test.js b/tests/qunit/ve-cite/ve.ui.MWReferenceSearchWidget.test.js index 414f64fab..b833ea472 100644 --- a/tests/qunit/ve-cite/ve.ui.MWReferenceSearchWidget.test.js +++ b/tests/qunit/ve-cite/ve.ui.MWReferenceSearchWidget.test.js @@ -11,7 +11,8 @@ function getInternalListMock( hasNode ) { default: return undefined; } }, - getAttributes: () => ( {} ) + getAttributes: () => ( {} ), + getInternalItem: () => ( {} ) } : {}; const groups = hasNode ? { 'mwReference/': {