-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
codeBlockGadget.js
104 lines (85 loc) 路 2.77 KB
/
codeBlockGadget.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
import $ from 'jquery';
import BlockOverlay from './blockOverlay';
const EVENT_LANGUAGE_CHANGED = 'language-changed';
const GADGET_WIDTH = 250;
const GADGET_HEIGHT = 30;
/**
* Class CodeBlockGadget
* @extends {BlockOverlay}
*/
class CodeBlockGadget extends BlockOverlay {
/**
* Creates an instance of CodeBlockGadget.
* @param {Object} options - options
* @param {EventManager} options.eventManager - event manager instance
* @param {HTMLElement} options.container - container element
* @param {WysiwygEditor} options.wysiwygEditor - wysiwyg editor instance
* @memberof CodeBlockGadget
*/
constructor({eventManager, container, wysiwygEditor}) {
super({
eventManager,
container,
attachedSelector: 'pre'
});
this._wysiwygEditor = wysiwygEditor;
this._popupCodeBlockLanguages = null;
this._initDOM();
this._initDOMEvent();
}
_initDOM() {
this.$el.addClass('code-block-header');
this._$languageLabel = $('<span>text</span>');
this.$el.append(this._$languageLabel);
this._$buttonOpenModalEditor = $(`<button>Editor</button>`);
this.$el.append(this._$buttonOpenModalEditor);
}
_initDOMEvent() {
this._$buttonOpenModalEditor.on('click', () => this._openPopupCodeBlockEditor());
}
_openPopupCodeBlockEditor() {
this._eventManager.emit('openPopupCodeBlockEditor', this.getAttachedElement());
}
_updateLanguage() {
const attachedElement = this.getAttachedElement();
const language = attachedElement ? attachedElement.getAttribute('data-language') : null;
this._$languageLabel.text(language ? language : 'text');
}
/**
* update gadget position
* @memberof CodeBlockGadget
* @protected
* @override
*/
syncLayout() {
const $attachedElement = $(this.getAttachedElement());
const offset = $attachedElement.offset();
offset.left = offset.left + ($attachedElement.outerWidth() - GADGET_WIDTH);
this.$el.offset(offset);
this.$el.height(GADGET_HEIGHT);
this.$el.width(GADGET_WIDTH);
}
/**
* on show
* @memberof CodeBlockGadget
* @protected
* @override
*/
onShow() {
super.onShow();
this._onAttachedElementChange = () => this._updateLanguage();
$(this.getAttachedElement()).on(EVENT_LANGUAGE_CHANGED, this._onAttachedElementChange);
this._updateLanguage();
}
/**
* on hide
* @memberof CodeBlockGadget
* @protected
* @override
*/
onHide() {
$(this.getAttachedElement()).off(EVENT_LANGUAGE_CHANGED, this._onAttachedElementChange);
super.onHide();
}
}
export default CodeBlockGadget;