Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Allow for unbinding single elements from a marker name in Mapper #1759

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/conversion/downcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,8 @@ function removeUIElement() {
return;
}

conversionApi.mapper.unbindElementsFromMarkerName( data.markerName );

for ( const element of elements ) {
conversionApi.mapper.unbindElementFromMarkerName( element, data.markerName );
conversionApi.writer.clear( conversionApi.writer.createRangeOn( element ), element );
}

Expand Down Expand Up @@ -1030,9 +1029,9 @@ function removeHighlight( highlightDescriptor ) {
return;
}

conversionApi.mapper.unbindElementsFromMarkerName( data.markerName );

for ( const element of elements ) {
conversionApi.mapper.unbindElementFromMarkerName( element, data.markerName );

if ( element.is( 'attributeElement' ) ) {
conversionApi.writer.unwrap( conversionApi.writer.createRangeOn( element ), viewHighlightElement );
} else {
Expand Down
15 changes: 12 additions & 3 deletions src/conversion/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,21 @@ export default class Mapper {
}

/**
* Unbinds all elements from given marker name.
* Unbinds an element from given marker name.
*
* @param {module:engine/view/element~Element} element Element to unbind.
* @param {String} name Marker name.
*/
unbindElementsFromMarkerName( name ) {
this._markerNameToElements.delete( name );
unbindElementFromMarkerName( element, name ) {
const elements = this._markerNameToElements.get( name );

if ( elements ) {
elements.delete( element );

if ( elements.size == 0 ) {
this._markerNameToElements.delete( name );
}
}
}

/**
Expand Down
17 changes: 11 additions & 6 deletions tests/conversion/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,20 +643,25 @@ describe( 'Mapper', () => {
expect( elements ).to.deep.equal( [ viewA, viewB, viewC ] );
} );

it( 'should unbind all elements from a marker name', () => {
it( 'should unbind element from a marker name', () => {
const viewA = new ViewElement( 'a' );
const viewB = new ViewElement( 'b' );
const viewC = new ViewElement( 'c' );

mapper.bindElementToMarker( viewA, 'marker' );
mapper.bindElementToMarker( viewB, 'marker' );
mapper.bindElementToMarker( viewC, 'marker' );

mapper.unbindElementsFromMarkerName( 'marker' );
mapper.unbindElementFromMarkerName( viewA, 'marker' );

const elements = mapper.markerNameToElements( 'marker' );
expect( Array.from( mapper.markerNameToElements( 'marker' ) ) ).to.deep.equal( [ viewB ] );

mapper.unbindElementFromMarkerName( viewB, 'marker' );

expect( mapper.markerNameToElements( 'marker' ) ).to.be.null;

// Removing an element from non-existing group or non-bound element should not cause a crash.
mapper.unbindElementFromMarkerName( viewB, 'marker' );

expect( elements ).to.be.null;
expect( mapper.markerNameToElements( 'marker' ) ).to.be.null;
} );
} );

Expand Down