2018-02-17 11:18:30 +00:00
|
|
|
import cledit from '../../services/cledit';
|
2017-11-04 16:59:48 +00:00
|
|
|
import editorSvc from '../../services/editorSvc';
|
|
|
|
import utils from '../../services/utils';
|
|
|
|
|
2017-11-10 23:39:51 +00:00
|
|
|
let savedSelection = null;
|
2017-11-04 16:59:48 +00:00
|
|
|
const nextTickCbs = [];
|
|
|
|
const nextTickExecCbs = cledit.Utils.debounce(() => {
|
|
|
|
while (nextTickCbs.length) {
|
|
|
|
nextTickCbs.shift()();
|
|
|
|
}
|
|
|
|
if (savedSelection) {
|
2017-11-10 23:39:51 +00:00
|
|
|
editorSvc.clEditor.selectionMgr.setSelectionStartEnd(
|
2018-05-06 00:46:33 +00:00
|
|
|
savedSelection.start,
|
|
|
|
savedSelection.end,
|
|
|
|
);
|
2017-11-04 16:59:48 +00:00
|
|
|
}
|
|
|
|
savedSelection = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const nextTick = (cb) => {
|
|
|
|
nextTickCbs.push(cb);
|
|
|
|
nextTickExecCbs();
|
|
|
|
};
|
|
|
|
|
|
|
|
const nextTickRestoreSelection = () => {
|
|
|
|
savedSelection = {
|
2017-11-10 23:39:51 +00:00
|
|
|
start: editorSvc.clEditor.selectionMgr.selectionStart,
|
|
|
|
end: editorSvc.clEditor.selectionMgr.selectionEnd,
|
2017-11-04 16:59:48 +00:00
|
|
|
};
|
|
|
|
nextTickExecCbs();
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class EditorClassApplier {
|
|
|
|
constructor(classGetter, offsetGetter, properties) {
|
|
|
|
this.classGetter = typeof classGetter === 'function' ? classGetter : () => classGetter;
|
|
|
|
this.offsetGetter = typeof offsetGetter === 'function' ? offsetGetter : () => offsetGetter;
|
|
|
|
this.properties = properties || {};
|
|
|
|
this.eltCollection = editorSvc.editorElt.getElementsByClassName(this.classGetter()[0]);
|
|
|
|
this.lastEltCount = this.eltCollection.length;
|
|
|
|
|
|
|
|
this.restoreClass = () => {
|
|
|
|
if (!this.eltCollection.length || this.eltCollection.length !== this.lastEltCount) {
|
|
|
|
this.removeClass();
|
|
|
|
this.applyClass();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 23:39:51 +00:00
|
|
|
editorSvc.clEditor.on('contentChanged', this.restoreClass);
|
2018-04-08 14:49:10 +00:00
|
|
|
nextTick(() => this.restoreClass());
|
2017-11-04 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyClass() {
|
2018-04-08 14:49:10 +00:00
|
|
|
if (!this.stopped) {
|
|
|
|
const offset = this.offsetGetter();
|
|
|
|
if (offset && offset.start !== offset.end) {
|
|
|
|
const range = editorSvc.clEditor.selectionMgr.createRange(
|
|
|
|
Math.min(offset.start, offset.end),
|
|
|
|
Math.max(offset.start, offset.end),
|
|
|
|
);
|
|
|
|
const properties = {
|
|
|
|
...this.properties,
|
|
|
|
className: this.classGetter().join(' '),
|
|
|
|
};
|
|
|
|
editorSvc.clEditor.watcher.noWatch(() => {
|
|
|
|
utils.wrapRange(range, properties);
|
|
|
|
});
|
|
|
|
if (editorSvc.clEditor.selectionMgr.hasFocus()) {
|
|
|
|
nextTickRestoreSelection();
|
|
|
|
}
|
|
|
|
this.lastEltCount = this.eltCollection.length;
|
2017-11-04 16:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeClass() {
|
2017-11-10 23:39:51 +00:00
|
|
|
editorSvc.clEditor.watcher.noWatch(() => {
|
2017-11-04 16:59:48 +00:00
|
|
|
utils.unwrapRange(this.eltCollection);
|
|
|
|
});
|
2017-11-10 23:39:51 +00:00
|
|
|
if (editorSvc.clEditor.selectionMgr.hasFocus()) {
|
2017-11-04 16:59:48 +00:00
|
|
|
nextTickRestoreSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2017-11-10 23:39:51 +00:00
|
|
|
editorSvc.clEditor.off('contentChanged', this.restoreClass);
|
2017-11-04 16:59:48 +00:00
|
|
|
nextTick(() => this.removeClass());
|
2018-04-08 14:49:10 +00:00
|
|
|
this.stopped = true;
|
2017-11-04 16:59:48 +00:00
|
|
|
}
|
|
|
|
}
|