2017-11-04 16:59:48 +00:00
|
|
|
import cledit from '../../libs/cledit';
|
|
|
|
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(
|
2017-11-04 16:59:48 +00:00
|
|
|
savedSelection.start, savedSelection.end);
|
|
|
|
}
|
|
|
|
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);
|
2017-11-04 16:59:48 +00:00
|
|
|
nextTick(() => this.applyClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
applyClass() {
|
|
|
|
const offset = this.offsetGetter();
|
|
|
|
if (offset && offset.start !== offset.end) {
|
2017-11-10 23:39:51 +00:00
|
|
|
const range = editorSvc.clEditor.selectionMgr.createRange(
|
2017-11-04 16:59:48 +00:00
|
|
|
Math.min(offset.start, offset.end),
|
|
|
|
Math.max(offset.start, offset.end),
|
|
|
|
);
|
|
|
|
const properties = {
|
|
|
|
...this.properties,
|
|
|
|
className: this.classGetter().join(' '),
|
|
|
|
};
|
2017-11-10 23:39:51 +00:00
|
|
|
editorSvc.clEditor.watcher.noWatch(() => {
|
2017-11-04 16:59:48 +00:00
|
|
|
utils.wrapRange(range, properties);
|
|
|
|
});
|
2017-11-10 23:39:51 +00:00
|
|
|
if (editorSvc.clEditor.selectionMgr.hasFocus()) {
|
2017-11-04 16:59:48 +00:00
|
|
|
nextTickRestoreSelection();
|
|
|
|
}
|
|
|
|
this.lastEltCount = this.eltCollection.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|