forked from ether/ep_headings2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (93 loc) · 3.85 KB
/
index.js
File metadata and controls
111 lines (93 loc) · 3.85 KB
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
var eejs = require('ep_etherpad-lite/node/eejs/');
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
var Security = require('ep_etherpad-lite/static/js/security');
var Security = require('ep_etherpad-lite/static/js/security');
var sceneMarkUtils = require("ep_script_scene_marks/utils");
// Define the styles so they are consistant between client and server
var style = eejs.require("ep_script_elements/static/css/editor.css")
var SCENE_LENGTH_NAMESPACE = 'scenesLength';
// Include CSS for HTML export
exports.stylesForExport = function(hook, padId, cb){
cb(style);
};
exports.getLineHTMLForExport = function (hook, context) {
var attribLine = context.attribLine;
var apool = context.apool;
var hasSceneMark = sceneMarkUtils.findSceneMarkAttribKey(context);
// if it is a scene mark it is not a script element (including a general)
if (hasSceneMark) return;
var text = context.lineContent;
// try to find a scene line attributes. if it's found it mounts the HTML with it
var scriptElement = findAttrib(attribLine, apool, "script_element");
if (scriptElement) {
// check if we have a `*` as the first character
if (!context.lineAttribsProcessed) {
text = text.substring(1);
context.lineAttribsProcessed = true;
}
// these dataAttributes refers do scene attributes like scene-name, scene-number, ...
var dataAttributes = mountAdditionalSceneData(context);
// finally, mount the HTML to export
context.lineContent = `<${scriptElement}>${dataAttributes}${text}</${scriptElement}>`;
} else if (!context.line.listLevel) { // if we are NOT inside a list
// HERE we handle elements that are neither a script element nor a scene
// mark. So, it's a fallback of general elements and we will wrap its
// content with <general> tag.
var fallbackTag = 'general';
// HOWEVER, if the line is empty (text has length 0), it means that we have
// a line break (<br>). As the Etherpad already joins all exported lines
// with <br>, so we don't need to wrap it.
// See: etherpad-lite/src/node/utils/ExportHtml.js
if (text.length > 0) {
context.lineContent = `<${fallbackTag}>${text}</${fallbackTag}>`;
}
}
}
exports.socketio = function(hook, context, cb) {
context.io
.of(`/${SCENE_LENGTH_NAMESPACE}`)
.on('connection', function(socket) {
socket.on('broadcastSceneLengthChange', function(data) {
var padId = data.padId;
var scenesLength = data.scenesLength;
// join the room
socket.join(padId);
// broadcast scenes length to all clients except sender
socket.broadcast.to(padId).emit('scenesLengthChanged', scenesLength);
})
});
cb();
}
//attrib is the element key in the pair key-value, scene-name:'whatever', in this case scene-name
function findAttrib(alineAttrs, apool, attrib) {
var script_element = null;
if (alineAttrs) {
var opIter = Changeset.opIterator(alineAttrs);
if (opIter.hasNext()) {
var op = opIter.next();
script_element = Changeset.opAttributeValue(op, attrib, apool);
}
}
return script_element;
}
//check if there's any scene tag as a lineattribute, if so return it formatted
function mountAdditionalSceneData(context) {
var sceneTag = ["scene-number", "scene-duration", "scene-temporality", "scene-workstate", "scene-index", "scene-time"];
var dataAttributes = "";
var sceneDataTags = "";
for (var i = 0; i < sceneTag.length; i++) {
var attribute = findAttrib(context.attribLine, context.apool, sceneTag[i]);
if (attribute){
dataAttributes += formatTagOutput(sceneTag[i],attribute);
}
}
if (dataAttributes){
sceneDataTags = "<scene"+dataAttributes+"></scene>";
}
return sceneDataTags;
}
//helper to output the sceneTag as tag="value"
function formatTagOutput(key, value) {
value = Security.escapeHTML(value);
return " "+key+"=\""+value+"\"";
}