This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scribe-plugin-code-command.js
62 lines (48 loc) · 1.88 KB
/
scribe-plugin-code-command.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
define(function () {
/**
* Adds a command for using CODEs.
*/
'use strict';
return function () {
return function (scribe) {
var codeCommand = new scribe.api.SimpleCommand('code', 'CODE');
codeCommand.execute = function () {
scribe.transactionManager.run(function () {
// TODO: When this command supports all types of ranges we can abstract
// it and use it for any command that applies inline styles.
var selection = new scribe.api.Selection();
var range = selection.range;
var selectedHtmlDocumentFragment = range.extractContents();
var codeElement = document.createElement('code');
codeElement.appendChild(selectedHtmlDocumentFragment);
range.insertNode(codeElement);
range.selectNode(codeElement);
// Re-apply the range
selection.selection.removeAllRanges();
selection.selection.addRange(range);
});
};
// There is no native command for CODE elements, so we have to provide
// our own `queryState` method.
// TODO: Find a way to make it explicit what the sequence of commands will
// be.
codeCommand.queryState = function () {
var selection = new scribe.api.Selection();
return !! selection.getContaining(function (node) {
return node.nodeName === this.nodeName;
}.bind(this));
};
// There is no native command for CODE elements, so we have to provide
// our own `queryEnabled` method.
// TODO: Find a way to make it explicit what the sequence of commands will
// be.
codeCommand.queryEnabled = function () {
var selection = new scribe.api.Selection();
var range = selection.range;
// TODO: Support uncollapsed ranges
return ! range.collapsed;
};
scribe.commands.code = codeCommand;
};
};
});