-
Notifications
You must be signed in to change notification settings - Fork 0
/
edi.js
135 lines (121 loc) · 3.79 KB
/
edi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
import {EdiUi} from './edi.ui.js';
import { Range } from './x/Range.js';
export class Edi {
range = {};
constructor(root){
root.addEventListener('focus',e=>{
if (this.active === e.target) return; // not changed (focus was on UI-element)
if (!this.isTarget(e.target)) return;
this.active = e.target;
e.target.dispatchEvent(new CustomEvent('edi-activate', {bubbles:true}));
},true);
root.addEventListener('blur',e=>{
if (!this.isTarget(e.target)) return;
if (this.dontTriggerBlur) return; // focus on UI-element
if (!this.active) {
console.warn('blur but not active???')
return;
}
e.target.dispatchEvent(new CustomEvent('edi-deactivate', {bubbles:true}));
this.active = false;
},true);
root.addEventListener('c1-selectionchange-target', e=>{
if (!this.isTarget(e.target)) return;
if (!this.active) return;
this.checkSelection();
// zB. qgSelection.rect triggers again, recursive
e.target.dispatchEvent(new CustomEvent('edi-selectionchange', {bubbles:true}));
}, true)
root.addEventListener('input',e=>{
if (!this.isTarget(e.target)) return;
//if (!this.active) return;
e.target.dispatchEvent(new CustomEvent('edi-input', {bubbles:true}));
},true);
/*
// esc to blur
root.addEventListener('keyup',e=>{
if (!this.isTarget(e.target)) return;
if (e.which === 27) { // needed?
document.body.focus();
document.activeElement.blur();
getSelection().removeAllRanges();
return;
}
},true);
root.addEventListener('beforeunload',()=>{ // blur before unload (save), needed?
this.active && this.active.blur();
},true);
*/
}
checkSelection() {
var range = Range.fromSelection();
this.range = range.oR;
let newElement = range.exactElement() || range.commonElement;
if (newElement === this.active) newElement = false;
this.element = newElement;
}
manipulate(fn) {
setTimeout(()=>{
new Range(this.range).select();
fn();
// selection gets lost:
//this.active.dispatchEvent(new CustomEvent('edi-input', {bubbles:true}));
this.active.focus(); // firefox
}, 80);
}
/*
modifySelection(fn) {
var els = [this.element];
if (!this.range.collapsed) {
console.log(this.range)
if (rangeIsElement(this.range)) {
els = [this.range.commonAncestorContainer];
} else {
els = rangeGetElements(this.range);
}
} else if (els[0] === this.active) {
rangeExpandToElements(this.range);
el = document.createElement('span');
this.range.surroundContents(el);
this.range.selectNodeContents(el);
}
fn(els);
this.trigger('input');
setTimeout(function() {
var s = getSelection();
s.c1SetRange(this.range);
this.checkSelection();
},90);
}
*/
addUiElement(el) { // really needed?
const activate = e => {
this.dontTriggerBlur = true;
const gMousedown = e=>{
if (el.contains(e.target)) return;
document.removeEventListener('mousedown',gMousedown);
this.dontTriggerBlur = false;
if (!this.active) return;
if (this.active.contains(e.target)) return;
this.active.dispatchEvent(new CustomEvent('edi-deactivate', {bubbles:true}));
this.active = false;
};
document.addEventListener('mousedown',gMousedown);
e.stopPropagation();
};
el.addEventListener('mousedown', activate);
el.addEventListener('mousedown', e=>e.stopPropagation());
el.addEventListener('touchstart', e=>e.stopPropagation());
}
isTarget(el) {
if (!el.isContentEditable) return;
//if ('form' in el) return; // needed?
var style = getComputedStyle(el);
var cpEdi = style.getPropertyValue('--edi'); // todo? cache cp-value or is it fast enough?
if (!cpEdi) return;
return true;
}
};
window.edi = new Edi(window);
window.edi.ui = new EdiUi(edi);