/
ez-btn-anchoredit.js
224 lines (173 loc) · 6.99 KB
/
ez-btn-anchoredit.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import AlloyEditor from 'alloyeditor';
const CLASS_HAS_ANCHOR = 'ez-has-anchor';
const CLASS_ICON_ANCHOR = 'ez-icon--anchor';
export default class EzBtnAnchorEdit extends Component {
constructor(props) {
super(props);
this.updateValue = this.updateValue.bind(this);
this.saveAnchor = this.saveAnchor.bind(this);
this.removeAnchor = this.removeAnchor.bind(this);
this.fireCustomUpdateEvent = this.fireCustomUpdateEvent.bind(this);
this.getStateClasses = AlloyEditor.ButtonStateClasses.getStateClasses;
this.block = null;
this.state = {
value: '',
isValueUnique: false,
};
}
componentDidMount() {
const block = this.findBlock();
const value = block.getId();
const isValueUnique = this.isValueUnique(value);
this.setState(() => ({ value, isValueUnique }));
}
static get key() {
return 'ezanchoredit';
}
findBlock() {
const nativeEditor = this.props.editor.get('nativeEditor');
const selected = nativeEditor.widgets.selected[0];
const path = nativeEditor.elementPath();
let block = path.block;
if (this.block) {
return this.block;
}
if (!block && selected) {
block = selected.element;
}
if (block && block.is('li')) {
block = block.getParent();
}
if (!block && path.contains('table')) {
block = path.elements.find((element) => element.is('table'));
}
this.block = block;
return block;
}
findIcon() {
const block = this.findBlock();
return [...block.getChildren().$].find((child) => child.classList && child.classList.contains(CLASS_ICON_ANCHOR));
}
updateValue({ nativeEvent }) {
const value = nativeEvent.target.value;
const isValueUnique = this.isValueUnique(value);
this.setState(() => ({ value, isValueUnique }));
}
isValueUnique(value) {
const block = this.findBlock();
return Object.values(CKEDITOR.instances).every((editor) => {
const data = editor.getData();
const container = document.createElement('div');
container.insertAdjacentHTML('afterbegin', data);
return (
value &&
[...container.querySelectorAll(`#${value}`)].every((element) => {
const ckeditorElement = new CKEDITOR.dom.element(element);
block.removeClass('is-block-focused');
return ckeditorElement.isIdentical(block);
})
);
});
}
fireCustomUpdateEvent() {
const nativeEditor = this.props.editor.get('nativeEditor');
nativeEditor.fire('customUpdate');
}
removeAnchor() {
const block = this.findBlock();
const icon = this.findIcon();
block.removeAttribute('id');
block.removeClass(CLASS_HAS_ANCHOR);
if (icon) {
icon.remove();
}
this.props.cancelExclusive();
block.focus();
this.fireCustomUpdateEvent();
}
saveAnchor() {
const { value } = this.state;
const block = this.findBlock();
const icon = this.findIcon();
block.setAttribute('id', value);
block.addClass(CLASS_HAS_ANCHOR);
if (!icon) {
this.renderIcon();
}
this.props.cancelExclusive();
block.focus();
this.fireCustomUpdateEvent();
}
renderIcon() {
const block = this.findBlock();
const container = document.createElement('div');
const icon = `
<svg class="ez-icon ez-icon--small ez-icon--secondary ${CLASS_ICON_ANCHOR}">
<use xlink:href="/bundles/ezplatformadminui/img/ez-icons.svg#link-anchor"></use>
</svg>`;
container.insertAdjacentHTML('afterbegin', icon);
const svg = new CKEDITOR.dom.element(container.querySelector('svg'));
block.append(svg, true);
}
renderError() {
const { value, isValueUnique } = this.state;
if (!value || isValueUnique) {
return null;
}
const errorMessage = Translator.trans(
/*@Desc("This anchor already exists on the page. Anchor name must be unique.")*/ 'anchor_btn.error.unique',
{},
'alloy_editor'
);
return <em className="ez-ae-anchor-edit__error">{errorMessage}</em>;
}
/**
* Lifecycle. Renders the UI of the button.
*
* @method render
* @return {Object} The content which should be rendered.
*/
render() {
const nameLabel = Translator.trans(/*@Desc("Name:")*/ 'anchor_edit.input.label', {}, 'alloy_editor');
const removeBtnTitle = Translator.trans(/*@Desc("Remove")*/ 'anchor_edit.btn.remove.title', {}, 'alloy_editor');
const saveBtnTitle = Translator.trans(/*@Desc("Save")*/ 'anchor_edit.btn.save.title', {}, 'alloy_editor');
const { value, isValueUnique } = this.state;
const isRemoveBtnDisabled = !value;
const isSaveBtnDisabled = !value || !isValueUnique;
return (
<div className="ez-ae-anchor-edit">
<div className="ez-ae-anchor-edit__input-wrapper">
<label className="ez-ae-anchor-edit__input-label">{nameLabel}</label>
<input type="text" className="ez-ae-anchor-edit__input form-control" onChange={this.updateValue} value={value} />
</div>
<div className="ez-ae-anchor-edit__actions">
<button
type="button"
title={removeBtnTitle}
className="ez-ae-anchor-edit__btn ez-ae-anchor-edit__btn--trash"
onClick={this.removeAnchor}
disabled={isRemoveBtnDisabled}>
<svg className="ez-icon ez-icon--light ez-icon--medium ez-btn-ae__icon">
<use xlinkHref="/bundles/ezplatformadminui/img/ez-icons.svg#trash" />
</svg>
</button>
<button
type="button"
title={saveBtnTitle}
className="ez-ae-anchor-edit__btn ez-ae-anchor-edit__btn--save"
onClick={this.saveAnchor}
disabled={isSaveBtnDisabled}>
<svg className="ez-icon ez-icon--light ez-icon--medium ez-btn-ae__icon">
<use xlinkHref="/bundles/ezplatformadminui/img/ez-icons.svg#checkmark" />
</svg>
</button>
</div>
{this.renderError()}
</div>
);
}
}
AlloyEditor.Buttons[EzBtnAnchorEdit.key] = AlloyEditor.EzBtnAnchorEdit = EzBtnAnchorEdit;
eZ.addConfig('ezAlloyEditor.ezBtnAnchorEdit', EzBtnAnchorEdit);