UNPKG

857 BJavaScriptView Raw
1"use strict";
2
3/**
4 * Return the current code block, from current selection or from a node key.
5 *
6 * @param {PluginOptions} opts
7 * @param {Slate.State} state
8 * @param {String} key?
9 * @return {Slate.Block || Void}
10 */
11function getCurrentCode(opts, state, key) {
12 var document = state.document;
13
14
15 var currentBlock = void 0;
16 if (key) {
17 currentBlock = state.document.getDescendant(key);
18 } else {
19 if (!state.selection.startKey) return null;
20 currentBlock = state.startBlock;
21 }
22
23 // The structure is always code_block -> code_line -> text
24 // So the parent of the currentBlock should be the code_block
25 var parent = document.getParent(currentBlock.key);
26 if (parent && parent.type === opts.containerType) {
27 return parent;
28 } else {
29 return null;
30 }
31}
32
33module.exports = getCurrentCode;
\No newline at end of file