+
+ onColorSelect(view, color) {
+ const attrs = {[this.attrName]: color};
+ const selection = view.state.selection;
+ const {from, to} = expandSelectionToMark(view.state, selection, this.markType);
+ const tr = view.state.tr;
+
+ const currentColorMarks = selection.$from.marksAcross(selection.$to) || [];
+ const activeRelevantMark = currentColorMarks.filter(mark => {
+ return mark.type === this.markType;
+ })[0];
+ const colorIsActive = activeRelevantMark && activeRelevantMark.attrs[this.attrName] === color;
+
+ tr.removeMark(from, to, this.markType);
+ if (!colorIsActive) {
+ tr.addMark(from, to, this.markType.create(attrs));
+ }
+
+ tr.setSelection(TextSelection.create(tr.doc, from, to));
+ view.dispatch(tr);
+ }