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

Commit

Permalink
Added more API options for setSelectionAttribute and removeSelectionA…
Browse files Browse the repository at this point in the history
…ttribute.
  • Loading branch information
ma2ciek committed Jan 21, 2018
1 parent 32beca0 commit 0991a41
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,24 @@ export default class DocumentSelection {
}

/**
* Removes all attributes from the selection.
*
* If there were any attributes in selection, fires the {@link #event:change} event with
* removed attributes' keys.
*
* @protected
*/
* @fires change:attribute
*/
_clearAttributes() {
this._selection.clearAttributes();
}

/**
* Returns an iterable that iterates through all selection attributes stored in current selection's parent.
*
* @protected
*/
* @returns {Iterable.<*>}
*/
_getStoredAttributes() {
return this._selection._getStoredAttributes();
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/liveselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export default class LiveSelection extends Selection {
/**
* Returns an iterable that iterates through all selection attributes stored in current selection's parent.
*
* @private
* @protected
* @returns {Iterable.<*>}
*/
* _getStoredAttributes() {
Expand Down
42 changes: 36 additions & 6 deletions src/model/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,30 @@ export default class Writer {
}

/**
* Sets attribute on the selection. If attribute with the same key already is set, it's value is overwritten.
* Sets attribute(s) on the selection. If attribute with the same key already is set, it's value is overwritten.
*
* @param {String} key Key of the attribute to set.
* @param {*} value Attribute value.
* @param {String|Object|Iterable.<*>} keyOrObjectOrIterable Key of the attribute to set
* or object / iterable of key - value attribute pairs.
* @param {*} [value] Attribute value.
*/
setSelectionAttribute( key, value ) {
setSelectionAttribute( keyOrObjectOrIterable, value ) {
this._assertWriterUsageCorrectness();

if ( typeof keyOrObjectOrIterable === 'string' ) {
this._setSelectionAttribute( keyOrObjectOrIterable, value );
} else {
for ( const [ key, value ] of toMap( keyOrObjectOrIterable ) ) {
this._setSelectionAttribute( key, value );
}
}
}

/**
* @private
* @param {String} key
* @param {*} value
*/
_setSelectionAttribute( key, value ) {
const selection = this.model.document.selection;

// Store attribute in parent element if the selection is collapsed in an empty node.
Expand All @@ -875,11 +891,25 @@ export default class Writer {
/**
* Removes an attribute with given key from the selection.
*
* @param {String} key Key of the attribute to remove.
* @param {String|Iterable.<String>} keyOrIterableOfKeys Key of the attribute to remove.
*/
removeSelectionAttribute( key ) {
removeSelectionAttribute( keyOrIterableOfKeys ) {
this._assertWriterUsageCorrectness();

if ( typeof keyOrIterableOfKeys === 'string' ) {
this._removeSelectionAttribute( keyOrIterableOfKeys );
} else {
for ( const key of keyOrIterableOfKeys ) {
this._removeSelectionAttribute( key );
}
}
}

/**
* @private
* @param {String} key Key of the attribute to remove.
*/
_removeSelectionAttribute( key ) {
const selection = this.model.document.selection;

// Remove stored attribute from parent element if the selection is collapsed in an empty node.
Expand Down

0 comments on commit 0991a41

Please sign in to comment.