-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (70 loc) · 2.61 KB
/
index.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
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var path = require('path')
, domOT = require('dom-ot')
, vdomToHtml = require('vdom-to-html')
, sanitizeHtml = require('sanitize-html')
module.exports = setup
module.exports.consumes = ['ui', 'ot', 'importexport', 'sync', 'orm']
function setup(plugin, imports, register) {
var ui = imports.ui
var ot = imports.ot
var importexport = imports.importexport
var sync = imports.sync
var orm = imports.orm
ui.registerModule(path.join(__dirname, 'client.js'))
ui.registerStylesheet(path.join(__dirname, 'index.css'))
ui.registerStaticDir(path.join(__dirname, 'ckeditor'))
ot.registerOTType('text/html', domOT)
importexport.registerExportProvider('text/html', 'text/html'
, function*(document, snapshot) {
return vdomToHtml(JSON.parse(snapshot.contents))
})
importexport.registerImportProvider('text/html', 'text/html'
, function*(document, user, data) {
var sanitizedHtml = sanitizeHtml(data, {
allowedTags: [ 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'img' ]
, allowedAttributes: {
a: [ 'href', 'name', 'target' ]
, img: [ 'src' ]
}
})
var importedTree = domOT.create('<div>'+sanitizedHtml+'</div>')
// get gulf doc and prepare changes
var gulfDoc = yield sync.getDocument(document.id)
if(!gulfDoc.initialized) {
yield function(cb) {
gulfDoc.once('init', cb)
}
}
var root = gulfDoc.content
, insertPath = [root.childNodes.length]
, changes = [new domOT.Move(null, insertPath, domOT.serialize(importedTree))]
var snapshot = yield orm.collections.snapshot
.findOne({id: document.latestSnapshot})
// commit changes
yield function(cb) {
gulfDoc.receiveEdit(JSON.stringify({
cs: JSON.stringify(changes)
, parent: snapshot.id
}), user.id, null, cb)
}
})
register()
}