Always apply inspected annotations to the right range

When you leave the inspector by changing the selection, we need to apply changes to the old selection.

ve.ui.Inspector
* Added initialSelection
* Change getMatchingAnnotations to use a given fragment rather than generating it's own
* Set initialSelection on open

ve.ui.Context
* Make hiding the context accept changes

ve.ui.LinkInspector
* Passing a fragment into getMatchingAnnotations now
* Using fragment API instead of actions API to control the range of the fragment

Change-Id: If6c8845285d87d0f144b15d50c38e192c797be59
This commit is contained in:
Trevor Parscal 2012-11-19 16:57:39 -08:00 committed by Catrope
parent a833691421
commit 05e39c1733
3 changed files with 18 additions and 11 deletions

View file

@ -76,7 +76,8 @@ ve.ui.LinkInspector.prototype.onLocationInputChange = function() {
*/ */
ve.ui.LinkInspector.prototype.onOpen = function () { ve.ui.LinkInspector.prototype.onOpen = function () {
var target = '', var target = '',
annotation = this.getMatchingAnnotations().get( 0 ); fragment = this.context.getSurface().getModel().getFragment(),
annotation = this.getMatchingAnnotations( fragment ).get( 0 );
if ( annotation ) { if ( annotation ) {
if ( annotation instanceof ve.dm.MWInternalLinkAnnotation ) { if ( annotation instanceof ve.dm.MWInternalLinkAnnotation ) {
@ -106,9 +107,11 @@ ve.ui.LinkInspector.prototype.onOpen = function () {
* @method * @method
*/ */
ve.ui.LinkInspector.prototype.onClose = function ( accept ) { ve.ui.LinkInspector.prototype.onClose = function ( accept ) {
var target = this.$locationInput.val(), var i, len, annotations,
target = this.$locationInput.val(),
surface = this.context.getSurface(), surface = this.context.getSurface(),
selection = surface.getModel().getSelection(); selection = surface.getModel().getSelection(),
fragment = surface.getModel().getFragment( this.initialSelection );
if ( accept && target && target !== this.initialTarget ) { if ( accept && target && target !== this.initialTarget ) {
if ( this.isNewAnnotation ) { if ( this.isNewAnnotation ) {
// Go back to before we add an annotation in prepareSelection // Go back to before we add an annotation in prepareSelection
@ -117,10 +120,12 @@ ve.ui.LinkInspector.prototype.onClose = function ( accept ) {
surface.execute( 'content', 'select', selection ); surface.execute( 'content', 'select', selection );
} else { } else {
// Clear all existing annotations // Clear all existing annotations
surface.execute( 'annotation', 'clearAll', this.constructor.static.typePattern ); annotations = this.getMatchingAnnotations( fragment ).get();
for ( i = 0, len = annotations.length; i < len; i++ ) {
fragment.annotateContent( 'clear', annotations[i] );
}
} }
// Apply the new annotation fragment.annotateContent( 'set', this.getAnnotationFromTarget( target ) );
surface.execute( 'annotation', 'set', this.getAnnotationFromTarget( target ) );
} }
this.isNewAnnotation = false; this.isNewAnnotation = false;
this.context.getSurface().getView().getDocument().getDocumentNode().$.focus(); this.context.getSurface().getView().getDocument().getDocumentNode().$.focus();
@ -145,7 +150,7 @@ ve.ui.LinkInspector.prototype.reset = function () {
*/ */
ve.ui.LinkInspector.prototype.prepareSelection = function () { ve.ui.LinkInspector.prototype.prepareSelection = function () {
var fragment = this.context.getSurface().getModel().getFragment(), var fragment = this.context.getSurface().getModel().getFragment(),
annotation = this.getMatchingAnnotations().get( 0 ); annotation = this.getMatchingAnnotations( fragment ).get( 0 );
if ( !annotation ) { if ( !annotation ) {
// Create annotation from selection // Create annotation from selection
fragment = fragment.trimRange(); fragment = fragment.trimRange();

View file

@ -194,7 +194,7 @@ ve.ui.Context.prototype.show = function () {
ve.ui.Context.prototype.hide = function () { ve.ui.Context.prototype.hide = function () {
if ( this.inspector ) { if ( this.inspector ) {
this.closeInspector( false ); this.closeInspector( true );
this.$overlay.hide(); this.$overlay.hide();
} }
if ( this.menu ) { if ( this.menu ) {

View file

@ -20,6 +20,7 @@ ve.ui.Inspector = function VeUiInspector( context ) {
// Properties // Properties
this.context = context; this.context = context;
this.disabled = false; this.disabled = false;
this.initialSelection = null;
this.frame = context.getFrame(); this.frame = context.getFrame();
this.$ = $( '<div class="ve-ui-inspector"></div>' ); this.$ = $( '<div class="ve-ui-inspector"></div>' );
this.$form = this.frame.$$( '<form></form>' ); this.$form = this.frame.$$( '<form></form>' );
@ -201,11 +202,11 @@ ve.ui.Inspector.prototype.prepareSelection = function () {
* Gets a list of matching annotations in selection. * Gets a list of matching annotations in selection.
* *
* @method * @method
* @param {ve.dm.SurfaceFragment} fragment Fragment to get matching annotations within
* @returns {ve.AnnotationSet} Matching annotations * @returns {ve.AnnotationSet} Matching annotations
*/ */
ve.ui.Inspector.prototype.getMatchingAnnotations = function () { ve.ui.Inspector.prototype.getMatchingAnnotations = function ( fragment ) {
return this.context.getSurface().getModel().getFragment().getAnnotations() return fragment.getAnnotations().getAnnotationsByName( this.constructor.static.typePattern );
.getAnnotationsByName( this.constructor.static.typePattern );
}; };
/** /**
@ -216,6 +217,7 @@ ve.ui.Inspector.prototype.getMatchingAnnotations = function () {
*/ */
ve.ui.Inspector.prototype.open = function () { ve.ui.Inspector.prototype.open = function () {
this.$.show(); this.$.show();
this.initialSelection = this.context.getSurface().getModel().getSelection();
this.onOpen(); this.onOpen();
this.emit( 'open' ); this.emit( 'open' );
}; };