-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
76 lines (68 loc) · 2.7 KB
/
client.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
/**
* 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 bindEditor = require('./methodDrawBinding')
module.exports = setup
module.exports.consumes = ['ui', 'editor']
module.exports.provides = []
function setup(plugin, imports, register) {
var editor = imports.editor
, ui = imports.ui
editor.registerEditor('Method-Draw', 'image/svg+xml', 'An easy to use, full-featured SVG editor'
, function(el, onClose) {
var iframe = document.createElement('iframe')
iframe.setAttribute('src', ui.baseURL+'/static/hive-editor-svg-method-draw/lib/Method-Draw/index.html')
// Maximize editor
el.style['height'] = '100%'
iframe.style['position'] = 'absolute'
iframe.style['border'] = 'none'
iframe.style['width'] = '0px' // hide initially until content is loaded
iframe.style['height'] = '0px'
window.addEventListener('scroll', updateFramePosition)
window.addEventListener('resize', updateFramePosition)
onClose(_ => {
window.removeEventListener('scroll', updateFramePosition)
window.removeEventListener('resize', updateFramePosition)
if(document.body.contains(iframe)) document.body.removeChild(iframe)
})
// load the editor
return new Promise(function(resolve) {
iframe.onload = function() {
resolve()
}
// we don't add it to #editor `el`, because vdom cannot handle iframes
document.body.appendChild(iframe)
})
.then(() => {
iframe.contentDocument.querySelector('#menu_bar').style['visibility'] = 'hidden'
// bind editor
var methodDraw = iframe.contentDocument.defaultView.methodDraw
, doc = bindEditor(methodDraw, iframe.contentDocument)
doc.once('editableInitialized', () => {
setImmediate(updateFramePosition) // Defer, because the loading bar disappears on the same event
})
return Promise.resolve(doc)
})
function updateFramePosition() {
var rect = el.getBoundingClientRect()
iframe.style['top'] = rect.top+'px'
iframe.style['left'] = rect.left+'px'
iframe.style['height'] = rect.height+'px'
iframe.style['width'] = rect.width+'px'
}
})
register()
}