-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselection.js
155 lines (130 loc) · 4.46 KB
/
selection.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
var $ = require('jquery');
var Prism = require('prismjs');
require('jquery-ui/ui/widgets/checkboxradio');
/**
* @class Selection
* @param {Object} config
* @param {Writer} config.writer
* @param {String} config.parentId
*/
function Selection(config) {
var w = config.writer;
var id = w.getUniqueId('selection_');
var enabled = true;
var selectionTrimLength = 500000;
var lastUpdate = new Date().getTime();
var showingFullDoc = false;
// w.utilities.addCSS('css/prism-ghcolors.css');
$('#'+config.parentId).append(`
<div class="moduleParent">
<div id="${id}" class="moduleContent"></div>
<div id="${id}" class="moduleFooter">
<label>Include RDF <input type="checkbox" name="includeRdf" /></label>
</div>
<div id="${id}_selectionContents" style="display: none;"></div>
</div>
`);
var $prismContainer = $('#'+id);
var $selectionContents = $('#'+id+'_selectionContents');
var $includeRdf = $('#'+id+' [name="includeRdf"]').checkboxradio();
$includeRdf.on('click', function(ev) {
updateView(true);
});
w.event('loadingDocument').subscribe(function() {
clearView();
});
w.event('selectionChanged').subscribe(function() {
if (!w.editor.selection.isCollapsed()) {
updateView();
} else if (!showingFullDoc) {
updateView(true);
}
});
w.event('contentChanged').subscribe(function() {
updateView(true);
});
w.event('nodeChanged').subscribe(function() {
if (!showingFullDoc) {
updateView();
}
});
w.event('tagSelected').subscribe(function() {
updateView();
});
w.event('tagAdded').subscribe(function() {
updateView(true);
});
w.event('tagEdited').subscribe(function() {
updateView(true);
});
w.event('tagRemoved').subscribe(function() {
updateView(true);
});
w.event('massUpdateStarted').subscribe(function() {
selection.disable();
});
w.event('massUpdateCompleted').subscribe(function() {
selection.enable(true);
});
/**
* @lends Selection.prototype
*/
var selection = {};
var updateView = function(useDoc) {
if (enabled) {
var timestamp = new Date().getTime();
var timeDiff = timestamp - lastUpdate; // track to avoid double update on nodeChanged/tagSelected combo
if ($prismContainer.is(':visible') && timeDiff > 250) {
lastUpdate = new Date().getTime();
if (useDoc || w.editor.selection.isCollapsed()) {
showingFullDoc = true;
var includeRdf = $includeRdf.prop('checked');
w.converter.getDocumentContent(includeRdf, _showString);
} else {
showingFullDoc = false;
var range = w.editor.selection.getRng(true);
var contents = range.cloneContents();
$selectionContents.html(contents);
var xmlString = w.converter.buildXMLString($selectionContents);
_showString(xmlString);
}
}
}
}
var _showString = function(xmlString) {
var escapedContents = w.utilities.escapeHTMLString(xmlString);
if (escapedContents.length > selectionTrimLength) {
escapedContents = escapedContents.substring(0, selectionTrimLength);// + '…';
}
$prismContainer.html(
'<pre style="width:100%;height:100%;padding:0;margin:0;border:none !important;"><code class="language-markup" style="white-space: pre-wrap;">'
+escapedContents+
'</code></pre>'
);
Prism.highlightElement($('code', $prismContainer)[0]);
}
var clearView = function() {
$prismContainer.html('');
}
selection.enable = function(forceUpdate) {
enabled = true;
if (forceUpdate) {
updateView(true);
}
}
selection.disable = function() {
enabled = false;
}
selection.showSelection = function() {
w.layoutManager.showModule('selection');
updateView(true);
};
selection.destroy = function() {
// TODO
}
// add to writer
w.selection = selection; // needed by view markup button
return selection;
};
module.exports = Selection;