-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfixed-menu-hack.js
62 lines (54 loc) · 1.75 KB
/
fixed-menu-hack.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
import _ from '../util/lodash'
let lastMenuHeight = 0
let boundSpaceContent = function () {}
let boundOnScroll = function () {}
const spaceContent = function (menuEl, contentEl) {
menuEl.style.minHeight = 'inherit'
const menuHeight = menuEl.offsetHeight
if (lastMenuHeight !== menuHeight) {
lastMenuHeight = menuHeight
contentEl.style.paddingTop = (menuHeight + 36) + 'px'
}
}
const onScroll = function (menuEl, contentEl) {
const contentTop = contentEl.getBoundingClientRect().top
if (contentTop > 0) {
menuEl.style.top = '0px'
return
}
menuEl.style.top = (0 - contentTop) + 'px'
}
export default {
view: function (editorView) {
// init after editor mounted
setTimeout(function () {
if (!editorView.content || !editorView.content.parentNode) return
const menuEl = editorView.content.parentNode.querySelector('.ProseMirror-menubar')
if (!menuEl) {
throw new Error('Trying to init FixedMenuHack without menu')
}
const contentEl = editorView.content
// Fake fixed
menuEl.style.position = 'absolute'
boundOnScroll = onScroll.bind(this, menuEl, contentEl)
window.addEventListener('scroll', boundOnScroll)
// Padding for content
boundSpaceContent = _.debounce(spaceContent, 100).bind(this, menuEl, contentEl)
window.addEventListener('resize', boundSpaceContent)
// init
lastMenuHeight = 0
boundSpaceContent()
boundOnScroll()
}, 0)
return {
update: function (editorView) {
boundSpaceContent()
},
destroy: function () {
// menuEl.style.position = 'inherit'
window.removeEventListener('scroll', boundOnScroll)
window.removeEventListener('resize', boundSpaceContent)
},
}
},
}