-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.js
37 lines (28 loc) · 912 Bytes
/
bubble.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
// @flow
import type { Document } from 'slate';
import { expand } from './Address';
import type { EditorEvent } from './events';
import type { Address } from './Address';
const addrKeyRe = /^addr_(\w*)_([0-9a-f]+)$/;
// Bubble an event emitting from address within document
export default function bubble(
evt: EditorEvent,
document: Document,
address: Address
): EditorEvent {
// hit the root -- return the event
if (address == null) {
return evt;
}
const node = document.getDescendant(address);
const reResults = addrKeyRe.exec(node.key);
const nextAddress = document.getParent(node).key;
// hit a structural node -- skip over it
if (reResults == null || reResults.length !== 3) {
return bubble(evt, document, nextAddress);
}
const key = reResults[2];
const term = expand(key);
const nextEvt = term.handle(evt);
return bubble(nextEvt, document, nextAddress);
}