From 340a939c97eda3a03dc1329cb7a75ae53367efe1 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 10 Apr 2014 11:55:24 -0700 Subject: [PATCH] Remove remaining uses of surfaceModel#getSelection in dialogs These were being used indirectly in the MW*Model's. Use surface fragments instead. Fixes I0fae3e5ff2bd. Change-Id: I1d6aa5e00a9315cf7088f87f9e9d828833feec64 --- .../ve-mw/dm/models/ve.dm.MWReferenceModel.js | 38 +++++++++---------- .../dm/models/ve.dm.MWTransclusionModel.js | 32 +++++++--------- .../init/targets/ve.init.mw.ViewPageTarget.js | 7 +++- .../ui/dialogs/ve.ui.MWCitationDialog.js | 8 ++-- .../ui/dialogs/ve.ui.MWReferenceDialog.js | 5 ++- .../ui/dialogs/ve.ui.MWTransclusionDialog.js | 5 ++- 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/modules/ve-mw/dm/models/ve.dm.MWReferenceModel.js b/modules/ve-mw/dm/models/ve.dm.MWReferenceModel.js index 93ec0306b9..e8f4904f93 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWReferenceModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWReferenceModel.js @@ -154,29 +154,25 @@ ve.dm.MWReferenceModel.prototype.updateInternalItem = function ( surfaceModel ) }; /** - * Insert reference into a surface. + * Insert reference at the end of a surface fragment. * - * Reference is inserted at the current cursor position in `surfaceModel`. - * - * @param {ve.dm.Surface} surfaceModel Surface model of main document - * @param {ve.Range} [at] Location to insert at + * @param {ve.dm.SurfaceFragment} surfaceModel Surface fragment to insert at */ -ve.dm.MWReferenceModel.prototype.insertReferenceNode = function ( surfaceModel, at ) { - surfaceModel - .getFragment( at || surfaceModel.getSelection().clone(), true ) - .collapseRangeToEnd() - .insertContent( [ - { - 'type': 'mwReference', - 'attributes': { - 'listKey': this.listKey, - 'listGroup': this.listGroup, - 'listIndex': this.listIndex, - 'refGroup': this.group - } - }, - { 'type': '/mwReference' } - ] ); +ve.dm.MWReferenceModel.prototype.insertReferenceNode = function ( surfaceFragment ) { + surfaceFragment + .collapseRangeToEnd() + .insertContent( [ + { + 'type': 'mwReference', + 'attributes': { + 'listKey': this.listKey, + 'listGroup': this.listGroup, + 'listIndex': this.listIndex, + 'refGroup': this.group + } + }, + { 'type': '/mwReference' } + ] ); }; /** diff --git a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js index c56a30c544..6b1689408e 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js @@ -49,26 +49,22 @@ OO.mixinClass( ve.dm.MWTransclusionModel, OO.EventEmitter ); /* Methods */ /** - * Insert transclusion into a surface. + * Insert transclusion at the end of a surface fragment. * - * Transclusion is inserted at the current cursor position in `surfaceModel`. - * - * @param {ve.dm.Surface} surfaceModel Surface model of main document - * @param {ve.Range} [at] Location to insert at + * @param {ve.dm.SurfaceFragment} surfaceModel Surface fragment to insert at */ -ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceModel, at ) { - surfaceModel - .getFragment( at || surfaceModel.getSelection().clone(), true ) - .collapseRangeToEnd() - .insertContent( [ - { - 'type': 'mwTransclusionInline', - 'attributes': { - 'mw': this.getPlainObject() - } - }, - { 'type': '/mwTransclusionInline' } - ] ); +ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment ) { + surfaceFragment + .collapseRangeToEnd() + .insertContent( [ + { + 'type': 'mwTransclusionInline', + 'attributes': { + 'mw': this.getPlainObject() + } + }, + { 'type': '/mwTransclusionInline' } + ] ); }; /** diff --git a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js index 02aa7b4dac..93cef934c2 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js +++ b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js @@ -715,11 +715,14 @@ ve.init.mw.ViewPageTarget.prototype.onToolbarMetaButtonClick = function () { * @param {ve.dm.Transaction} transaction */ ve.init.mw.ViewPageTarget.prototype.checkForWikitextWarning = function () { - var text, doc = this.surface.getView().getDocument(), + var text, node, doc = this.surface.getView().getDocument(), selection = this.surface.getModel().getSelection(), - node = doc.getNodeFromOffset( selection.start ), textMatches, viewPageTarget = this; + if ( !selection ) { + return; + } + node = doc.getNodeFromOffset( selection.start ); if ( !( node instanceof ve.ce.ContentBranchNode ) ) { return; } diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWCitationDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWCitationDialog.js index 1f1d9c1155..cb2b10656e 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWCitationDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWCitationDialog.js @@ -83,7 +83,8 @@ ve.ui.MWCitationDialog.prototype.getTransclusionNode = function () { */ ve.ui.MWCitationDialog.prototype.saveChanges = function () { var item, - surfaceModel = this.getFragment().getSurface(), + surfaceFragment = this.getFragment(), + surfaceModel = surfaceFragment.getSurface(), doc = surfaceModel.getDocument(), internalList = doc.getInternalList(), obj = this.transclusion.getPlainObject(); @@ -92,7 +93,7 @@ ve.ui.MWCitationDialog.prototype.saveChanges = function () { surfaceModel.getFragment().collapseRangeToEnd(); this.referenceModel = new ve.dm.MWReferenceModel(); this.referenceModel.insertInternalItem( surfaceModel ); - this.referenceModel.insertReferenceNode( surfaceModel ); + this.referenceModel.insertReferenceNode( surfaceFragment ); } item = this.referenceModel.findInternalItem( surfaceModel ); @@ -101,14 +102,13 @@ ve.ui.MWCitationDialog.prototype.saveChanges = function () { this.transclusion.updateTransclusionNode( surfaceModel, this.transclusionNode ); } else if ( obj !== null ) { this.transclusion.insertTransclusionNode( - surfaceModel, // HACK: This is trying to place the cursor inside the first content branch node // but this theoretically not a safe assumption - in practice, the citation dialog // will only reach this code if we are inserting (not updating) a transclusion, so // the referenceModel will have already initialized the internal node with a // paragraph - getting the range of the item covers the entire paragraph so we have // to get the range of it's first (and empty) child - item.getChildren()[0].getRange() + surfaceFragment.clone( item.getChildren()[0].getRange() ) ); } } diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWReferenceDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWReferenceDialog.js index ab9625425f..a07bc704bb 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWReferenceDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWReferenceDialog.js @@ -323,7 +323,8 @@ ve.ui.MWReferenceDialog.prototype.setup = function ( data ) { * @inheritdoc */ ve.ui.MWReferenceDialog.prototype.teardown = function ( data ) { - var surfaceModel = this.getFragment().getSurface(); + var surfaceFragment = this.getFragment(), + surfaceModel = surfaceFragment.getSurface(); // Data initialization data = data || {}; @@ -336,7 +337,7 @@ ve.ui.MWReferenceDialog.prototype.teardown = function ( data ) { if ( !this.referenceModel.findInternalItem( surfaceModel ) ) { this.referenceModel.insertInternalItem( surfaceModel ); } - this.referenceModel.insertReferenceNode( surfaceModel ); + this.referenceModel.insertReferenceNode( surfaceFragment ); } // Update internal item this.referenceModel.updateInternalItem( surfaceModel ); diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js index 99aac56d76..2b1d989dae 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js @@ -480,13 +480,14 @@ ve.ui.MWTransclusionDialog.prototype.getTransclusionNode = function () { * Save changes. */ ve.ui.MWTransclusionDialog.prototype.saveChanges = function () { - var surfaceModel = this.getFragment().getSurface(), + var surfaceFragment = this.getFragment(), + surfaceModel = surfaceFragment.getSurface(), obj = this.transclusion.getPlainObject(); if ( this.transclusionNode instanceof ve.dm.MWTransclusionNode ) { this.transclusion.updateTransclusionNode( surfaceModel, this.transclusionNode ); } else if ( obj !== null ) { - this.transclusion.insertTransclusionNode( surfaceModel ); + this.transclusion.insertTransclusionNode( surfaceFragment ); } };