editorjs-image 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,893 +0,0 @@
1
- // Class for Editor.js
2
- // Adds Block Tunes for Image Tool (ImageTool)
3
- // Supports : Image Float Left, Image float right
4
-
5
- import './image-tool-tune.css';
6
- import Cropper from 'cropperjs';
7
-
8
- export default class {
9
-
10
- constructor( { api, data, config, block } ) {
11
-
12
- this.settings = [
13
- {
14
- name: 'floatLeft',
15
- icon: '<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M952 792H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0-632H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM608 660c8.8 0 16-7.2 16-16V380c0-8.8-7.2-16-16-16H96c-8.8 0-16 7.2-16 16v264c0 8.8 7.2 16 16 16h512zM152 436h400v152H152V436zm552 210c0 4.4 3.6 8 8 8h224c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H712c-4.4 0-8 3.6-8 8v56zm8-204h224c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H712c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8z"></path></svg>',
16
- label: '',
17
- group: 'align',
18
- }, {
19
- name: 'center',
20
- icon: '<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M952 792H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0-632H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM848 660c8.8 0 16-7.2 16-16V380c0-8.8-7.2-16-16-16H176c-8.8 0-16 7.2-16 16v264c0 8.8 7.2 16 16 16h672zM232 436h560v152H232V436z"></path></svg>',
21
- label: '',
22
- group: 'align',
23
- }, {
24
- name: 'floatRight',
25
- icon: '<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M952 792H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0-632H72c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h880c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm-24 500c8.8 0 16-7.2 16-16V380c0-8.8-7.2-16-16-16H416c-8.8 0-16 7.2-16 16v264c0 8.8 7.2 16 16 16h512zM472 436h400v152H472V436zM80 646c0 4.4 3.6 8 8 8h224c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H88c-4.4 0-8 3.6-8 8v56zm8-204h224c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H88c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8z"></path></svg>',
26
- label: '',
27
- group: 'align',
28
- },
29
- {
30
- name: 'sizeSmall',
31
- icon: '<svg stroke="currentColor" fill="none" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M12 6V4H20V20H12V18H8V16H4V8H8V6H12ZM14 6H18V18H14V6ZM12 8H10V16H12V8ZM8 10V14H6V10H8Z" fill="currentColor"></path></svg>',
32
- label: '50%',
33
- group: 'size',
34
- }, {
35
- name: 'sizeMiddle',
36
- icon: '<svg stroke="currentColor" fill="none" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M12 6V4H20V20H12V18H8V16H4V8H8V6H12ZM14 6H18V18H14V6ZM12 8H10V16H12V8ZM8 10V14H6V10H8Z" fill="currentColor"></path></svg>',
37
- label: '70%',
38
- group: 'size',
39
- }, {
40
- name: 'sizeLarge',
41
- icon: '<svg stroke="currentColor" fill="none" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M12 6V4H20V20H12V18H8V16H4V8H8V6H12ZM14 6H18V18H14V6ZM12 8H10V16H12V8ZM8 10V14H6V10H8Z" fill="currentColor"></path></svg>',
42
- label: '100%',
43
- group: 'size',
44
- }, {
45
- name: 'resize',
46
- icon: '<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M29 30l1 90h36V66h26V30H29zm99 0v36h72V30h-72zm108 0v36h72V30h-72zm108 0v36h72V30h-72zm102 0v78h36V30h-36zm-206 80v36h100.543l-118 118H30v218h218V289.457l118-118V272h36V110H240zm206 34v72h36v-72h-36zM30 156v72h36v-72H30zm416 96v72h36v-72h-36zm0 108v72h36v-72h-36zm-166 86v36h72v-36h-72zm108 0v36h72v-36h-72z"></path></svg>',
47
- label: '',
48
- group: 'size',
49
- }, {
50
- name: 'crop',
51
- icon: '<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M21 15h2v2h-2v-2zm0-4h2v2h-2v-2zm2 8h-2v2c1 0 2-1 2-2zM13 3h2v2h-2V3zm8 4h2v2h-2V7zm0-4v2h2c0-1-1-2-2-2zM1 7h2v2H1V7zm16-4h2v2h-2V3zm0 16h2v2h-2v-2zM3 3C2 3 1 4 1 5h2V3zm6 0h2v2H9V3zM5 3h2v2H5V3zm-4 8v8c0 1.1.9 2 2 2h12V11H1zm2 8l2.5-3.21 1.79 2.15 2.5-3.22L13 19H3z"></path></svg>',
52
- label: '',
53
- group: 'size',
54
- }
55
- ];
56
-
57
- this.api = api;
58
- this.block = block;
59
- this.config = config;
60
- this.data = {
61
- floatLeft: data !== undefined && data.floatLeft !== undefined ? data.floatLeft : false,
62
- floatRight: data !== undefined && data.floatRight !== undefined ? data.floatRight : false,
63
- center: data !== undefined && data.center !== undefined ? data.center : false,
64
- sizeSmall: data !== undefined && data.sizeSmall !== undefined ? data.sizeSmall : false,
65
- sizeMiddle: data !== undefined && data.sizeMiddle !== undefined ? data.sizeMiddle : false,
66
- sizeLarge: data !== undefined && data.sizeLarge !== undefined ? data.sizeLarge : false,
67
- resize: data !== undefined && data.resize !== undefined ? data.resize : false,
68
- resizeSize: data !== undefined && data.resizeSize !== undefined ? data.resizeSize : 0,
69
- crop: data !== undefined && data.crop !== undefined ? data.crop : false,
70
- cropperFrameHeight: data !== undefined && data.cropperFrameHeight !== undefined ? data.cropperFrameHeight : 0,
71
- cropperFrameWidth: data !== undefined && data.cropperFrameWidth !== undefined ? data.cropperFrameWidth : 0,
72
- cropperFrameLeft: data !== undefined && data.cropperFrameLeft !== undefined ? data.cropperFrameLeft : 0,
73
- cropperFrameTop: data !== undefined && data.cropperFrameTop !== undefined ? data.cropperFrameTop : 0,
74
- cropperImageHeight: data !== undefined && data.cropperImageHeight !== undefined ? data.cropperImageHeight : 0,
75
- cropperImageWidth: data !== undefined && data.cropperImageWidth !== undefined ? data.cropperImageWidth : 0,
76
- cropperInterface: undefined,
77
- };
78
- this.wrapper = undefined;
79
-
80
- this.buttons = [];
81
-
82
- }
83
-
84
- static get isTune() {
85
- return true;
86
- }
87
-
88
- static get sanitize() {
89
- return {
90
- floatLeft: {},
91
- floatRight: {},
92
- center: {},
93
- sizeSmall: {},
94
- sizeMiddle: {},
95
- sizeLarge: {},
96
- resize: {},
97
- resizeSize: {},
98
- crop: {},
99
- cropperFrameHeight: {},
100
- cropperFrameWidth: {},
101
- cropperFrameLeft: {},
102
- cropperFrameTop: {},
103
- cropperImageHeight: {},
104
- cropperImageWidth: {},
105
- cropperInterface: {},
106
- };
107
- }
108
-
109
- /**
110
- * CSS classes
111
- * @return {object}
112
- * @constructor
113
- * @property {string} CSS.wrapper - wrapper for buttons
114
- * @property {string} CSS.button - button
115
- * @property {string} CSS.buttonActive - active button
116
- * @property {string} CSS.buttonModifier - button with modifier
117
- * @property {string} CSS.buttonModifierActive - active button with modifier
118
- */
119
- get CSS() {
120
- return {
121
- wrapper: 'cdx-image-tool-tune',
122
- button: this.api.styles.settingsButton,
123
- buttonActive: this.api.styles.settingsButtonActive,
124
- buttonModifier: this.api.styles.settingsButtonModifier,
125
- buttonModifierActive: this.api.styles.settingsButtonModifierActive,
126
- isFloatLeft: 'cdx-image-tool-tune--floatLeft',
127
- isFloatRight: 'cdx-image-tool-tune--floatRight',
128
- isCenter: 'cdx-image-tool-tune--center',
129
- isSizeSmall: 'cdx-image-tool-tune--sizeSmall',
130
- isSizeMiddle: 'cdx-image-tool-tune--sizeMiddle',
131
- isSizeLarge: 'cdx-image-tool-tune--sizeLarge',
132
- isResize: 'cdx-image-tool-tune--resize',
133
- isCrop: 'cdx-image-tool-tune--crop',
134
- };
135
- }
136
-
137
- /**
138
- *
139
- * @return {HTMLElement}
140
- * @public
141
- * @readonly
142
- * @property {HTMLElement} wrapper - tune buttons wrapper
143
- */
144
- get view() {
145
- if ( !this.wrapper ) {
146
- this.wrapper = this.createView();
147
- }
148
-
149
- return this.wrapper;
150
- }
151
-
152
- /**
153
- * Clicks to one of the tunes
154
- * @param {MouseEvent} e - click
155
- * @param {HTMLElement} tune - clicked tune button
156
- * @private
157
- * @return {void}
158
- * */
159
- tuneClicked( e, tune ) {
160
- e.preventDefault();
161
- e.stopPropagation();
162
-
163
-
164
- let tuneName = tune.dataset.tune;
165
-
166
- let tuneGroup = this.settings.find( tune => tune.name === tuneName ).group;
167
-
168
-
169
- this.buttons.forEach( button => {
170
-
171
- //if is the same group
172
- if ( this.settings.find( tune => tune.name === button.dataset.tune ).group === tuneGroup ) {
173
- if ( button !== tune ) {
174
- button.classList.remove( this.CSS.buttonActive );
175
- }
176
- }
177
- } );
178
-
179
- tune.classList.toggle( this.CSS.buttonActive );
180
-
181
-
182
- this.setTune( tune.dataset.tune );
183
- }
184
-
185
- /**
186
- * Styles the image with a tune
187
- * @param {string} tune - tune name
188
- * @private
189
- * @return {void}
190
- * */
191
- setTune( tune ) {
192
- switch ( tune ) {
193
- case 'floatLeft':
194
- this.data.floatLeft = !this.data.floatLeft;
195
- this.data.floatRight = false;
196
- this.data.center = false;
197
- break;
198
- case 'floatRight':
199
- this.data.floatLeft = false;
200
- this.data.floatRight = !this.data.floatRight;
201
- this.data.center = false;
202
- break;
203
- case 'center':
204
- this.data.center = !this.data.center;
205
- this.data.floatLeft = false;
206
- this.data.floatRight = false;
207
- break;
208
- case 'sizeSmall':
209
- this.data.sizeSmall = !this.data.sizeSmall;
210
- this.data.sizeMiddle = false;
211
- this.data.sizeLarge = false;
212
- this.data.resize = false;
213
- this.data.crop = false;
214
- break;
215
- case 'sizeMiddle':
216
- this.data.sizeSmall = false;
217
- this.data.sizeMiddle = !this.data.sizeMiddle;
218
- this.data.sizeLarge = false;
219
- this.data.resize = false;
220
- this.data.crop = false;
221
- break;
222
- case 'sizeLarge':
223
- this.data.sizeSmall = false;
224
- this.data.sizeMiddle = false;
225
- this.data.sizeLarge = !this.data.sizeLarge;
226
- this.data.resize = false;
227
- this.data.crop = false;
228
- break;
229
- case 'resize':
230
- this.data.sizeSmall = false;
231
- this.data.sizeMiddle = false;
232
- this.data.sizeLarge = false;
233
- this.data.resize = !this.data.resize;
234
- this.data.crop = false;
235
- break;
236
- case 'crop':
237
- this.data.crop = !this.data.crop;
238
- this.data.sizeSmall = false;
239
- this.data.sizeMiddle = false;
240
- this.data.sizeLarge = false;
241
- this.data.resize = false;
242
- this.data.resizeSize = 0;
243
- break;
244
- default:
245
- this.data.floatLeft = false;
246
- this.data.floatRight = false;
247
- this.data.sizeSmall = false;
248
- this.data.sizeMiddle = false;
249
- this.data.sizeLarge = false;
250
- this.data.resize = false;
251
- this.data.crop = false;
252
- break;
253
- }
254
-
255
- if ( !this.data.resize ) {
256
- this.data.resizeSize = 0;
257
- }
258
-
259
- if ( !this.data.crop ) {
260
- this.data.cropperFrameHeight = 0;
261
- this.data.cropperFrameWidth = 0;
262
- this.data.cropperFrameLeft = 0;
263
- this.data.cropperFrameTop = 0;
264
- this.data.cropperImageHeight = 0;
265
- this.data.cropperImageWidth = 0;
266
- }
267
-
268
- const blockContent = this.block.holder.querySelector( '.ce-block__content' );
269
-
270
- this.apply( blockContent );
271
-
272
- this.block.dispatchChange();
273
-
274
-
275
-
276
- }
277
-
278
-
279
- /**
280
- * Append class to block by tune data
281
- * @param {HTMLElement} blockContent - wrapper for block content
282
- * @public
283
- * @return {void}
284
- * */
285
- apply( blockContent ) {
286
-
287
-
288
- if ( this.data.floatLeft ) {
289
- blockContent.classList.add( this.CSS.isFloatLeft );
290
- } else {
291
- blockContent.classList.remove( this.CSS.isFloatLeft );
292
- }
293
-
294
- if ( this.data.floatRight ) {
295
- blockContent.classList.add( this.CSS.isFloatRight );
296
- } else {
297
- blockContent.classList.remove( this.CSS.isFloatRight );
298
- }
299
-
300
- if ( this.data.center ) {
301
- blockContent.classList.add( this.CSS.isCenter );
302
- } else {
303
- blockContent.classList.remove( this.CSS.isCenter );
304
- }
305
-
306
- if ( this.data.sizeSmall ) {
307
- blockContent.classList.add( this.CSS.isSizeSmall );
308
- } else {
309
- blockContent.classList.remove( this.CSS.isSizeSmall );
310
- }
311
-
312
- if ( this.data.sizeMiddle ) {
313
- blockContent.classList.add( this.CSS.isSizeMiddle );
314
- } else {
315
- blockContent.classList.remove( this.CSS.isSizeMiddle );
316
- }
317
-
318
- if ( this.data.sizeLarge ) {
319
- blockContent.classList.add( this.CSS.isSizeLarge );
320
- } else {
321
- blockContent.classList.remove( this.CSS.isSizeLarge );
322
- }
323
-
324
- if ( this.data.resize ) {
325
- blockContent.classList.add( this.CSS.isResize );
326
-
327
- if ( this.data.resizeSize > 0 ) {
328
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.width = this.data.resizeSize + 'px';
329
- }
330
-
331
- this.resize( blockContent );
332
- } else {
333
- blockContent.classList.remove( this.CSS.isResize );
334
- this.unresize( blockContent );
335
- }
336
-
337
- if ( this.data.crop ) {
338
- blockContent.classList.add( this.CSS.isCrop );
339
-
340
- this.crop( blockContent );
341
- if ( this.data.cropperFrameHeight > 0 && this.data.cropperFrameWidth > 0 ) {
342
- this.applyCrop( blockContent );
343
- }
344
-
345
-
346
- } else {
347
- blockContent.classList.remove( this.CSS.isCrop );
348
- this.uncrop( blockContent );
349
- }
350
-
351
-
352
-
353
-
354
- }
355
-
356
-
357
-
358
-
359
- /**
360
- * Add crop handles to image
361
- * @param {HTMLCollection} images - images in block
362
- * @public
363
- * @return {void}
364
- */
365
- crop( blockContent ) {
366
-
367
- //this.appendCrop( blockContent );
368
-
369
-
370
-
371
- //add append crop button to image-tool__image
372
- const image = blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ];
373
- const cropBtn = document.createElement( 'div' );
374
- cropBtn.classList.add( 'crop-btn', 'btn-crop-action' );
375
- cropBtn.innerHTML = 'Crop';
376
-
377
- cropBtn.addEventListener( 'click', e => {
378
- //remove crop button
379
- image.removeChild( cropBtn );
380
- this.appendCrop( blockContent );
381
- }
382
- );
383
-
384
- image.appendChild( cropBtn );
385
-
386
- }
387
-
388
-
389
- appendCrop( blockContent ) {
390
- this.uncrop( blockContent );
391
- const image = blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].getElementsByTagName( 'img' )[ 0 ];
392
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].classList.add( 'isCropping' );
393
- this.cropperInterface = new Cropper( image, {
394
- crop( event ) {
395
- console.log( event.detail.x );
396
- console.log( event.detail.y );
397
- console.log( event.detail.width );
398
- console.log( event.detail.height );
399
- console.log( event.detail.scaleX );
400
- console.log( event.detail.scaleY );
401
-
402
-
403
- },
404
- } );
405
-
406
-
407
- //append save crop button
408
- const cropSaveBtn = document.createElement( 'div' );
409
- cropSaveBtn.classList.add( 'crop-save', 'btn-crop-action' );
410
-
411
- cropSaveBtn.innerHTML = 'Apply';
412
-
413
- cropSaveBtn.addEventListener( 'click', e => {
414
-
415
- console.log( this.cropperInterface.getCropBoxData() );
416
- console.log( this.cropperInterface.getImageData() );
417
- console.log( this.cropperInterface.getCanvasData() );
418
-
419
-
420
- this.data.cropperFrameHeight = this.cropperInterface.getCropBoxData().height;
421
- this.data.cropperFrameWidth = this.cropperInterface.getCropBoxData().width;
422
- this.data.cropperFrameLeft = this.cropperInterface.getCanvasData().left - this.cropperInterface.getCropBoxData().left;
423
- this.data.cropperFrameTop = this.cropperInterface.getCanvasData().top - this.cropperInterface.getCropBoxData().top;
424
- this.data.cropperImageHeight = this.cropperInterface.getImageData().height;
425
- this.data.cropperImageWidth = this.cropperInterface.getImageData().width;
426
-
427
- this.applyCrop( blockContent );
428
-
429
- }
430
- );
431
-
432
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].appendChild( cropSaveBtn );
433
-
434
- //add temporary style to block content so that it comes in front of every other block
435
- blockContent.classList.add( 'isCropping' );
436
-
437
- }
438
-
439
-
440
- applyCrop( blockContent ) {
441
-
442
- //apply data to image and remove cropper interface and save button, add crop button
443
-
444
- const image = blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].getElementsByTagName( 'img' )[ 0 ];
445
-
446
-
447
-
448
-
449
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.minWidth = this.data.cropperFrameWidth + 'px';
450
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.maxWidth = this.data.cropperFrameWidth + 'px';
451
-
452
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.width = this.data.cropperFrameWidth + 'px';
453
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.height = this.data.cropperFrameHeight + 'px';
454
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.left = this.data.cropperFrameLeft + 'px';
455
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.top = this.data.cropperFrameTop + 'px';
456
-
457
- image.style.width = this.data.cropperImageWidth + 'px';
458
- image.style.height = this.data.cropperImageHeight + 'px';
459
-
460
-
461
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].classList.add( 'isCropped' );
462
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].classList.remove( 'isCropping' );
463
-
464
-
465
- const cropSaveBtn = blockContent.getElementsByClassName( 'btn-crop-action' )[ 0 ];
466
- if ( cropSaveBtn ) {
467
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].removeChild( cropSaveBtn );
468
- }
469
-
470
-
471
-
472
- //remove cropper interface
473
- if ( this.cropperInterface ) {
474
- this.cropperInterface.destroy();
475
- }
476
-
477
-
478
-
479
-
480
- //add crop button
481
-
482
- const cropBtn = document.createElement( 'div' );
483
- cropBtn.classList.add( 'crop-btn', 'btn-crop-action' );
484
- cropBtn.innerHTML = 'Crop';
485
-
486
- cropBtn.addEventListener( 'click', e => {
487
- //remove crop button
488
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].removeChild( cropBtn );
489
- this.appendCrop( blockContent );
490
- }
491
- );
492
-
493
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].appendChild( cropBtn );
494
-
495
- blockContent.classList.remove( 'isCropping' );
496
-
497
- this.block.dispatchChange();
498
-
499
- }
500
-
501
-
502
- uncrop( blockContent ) {
503
-
504
- //remove crop and save button
505
- const cropSaveBtn = blockContent.getElementsByClassName( 'btn-crop-action' )[ 0 ];
506
- if ( cropSaveBtn ) {
507
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].removeChild( cropSaveBtn );
508
- }
509
-
510
-
511
- //remove crop button
512
- const cropBtn = blockContent.getElementsByClassName( 'btn-crop-action' )[ 0 ];
513
- if ( cropBtn ) {
514
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].removeChild( cropBtn );
515
- }
516
-
517
- //remove isCropped class
518
-
519
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].classList.remove( 'isCropped' );
520
-
521
- //remove isCropping class
522
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].classList.remove( 'isCropping' );
523
-
524
- //remove min and max width
525
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.minWidth = '';
526
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.maxWidth = '';
527
-
528
- //remove image width and height
529
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.width = '';
530
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.height = '';
531
-
532
- //remove image left and top
533
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.left = '';
534
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.top = '';
535
-
536
- //remove image width and height
537
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.width = '';
538
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.height = '';
539
-
540
-
541
-
542
- blockContent.classList.remove( 'isCropping' );
543
-
544
-
545
-
546
- //remove cropper interface
547
- if ( this.cropperInterface ) {
548
- this.cropperInterface.destroy();
549
- }
550
-
551
- //remove crop data
552
- this.data.cropperFrameHeight = 0;
553
- this.data.cropperFrameWidth = 0;
554
- this.data.cropperFrameLeft = 0;
555
- this.data.cropperFrameTop = 0;
556
- this.data.cropperImageHeight = 0;
557
- this.data.cropperImageWidth = 0;
558
-
559
- this.block.dispatchChange();
560
-
561
- }
562
-
563
-
564
-
565
-
566
-
567
- /**
568
- * Add resize handles to block
569
- * @param {HTMLElement} blockContent - wrapper for block content
570
- * @public
571
- * @return {void}
572
- * */
573
- resize( blockContent ) {
574
-
575
-
576
- const resizable = document.createElement( 'div' );
577
- resizable.classList.add( 'resizable' );
578
-
579
- console.log( resizable );
580
-
581
-
582
- const resizers = document.createElement( 'div' );
583
- resizers.classList.add( 'resizers' );
584
-
585
- const resizerTopLeft = document.createElement( 'div' );
586
- resizerTopLeft.classList.add( 'resizer', 'top-left' );
587
- resizerTopLeft.addEventListener( 'mousedown', e => {
588
- this.resizeClick( blockContent.getElementsByClassName( 'cdx-block' )[ 0 ], resizerTopLeft, e );
589
- } );
590
-
591
-
592
- const resizerTopRight = document.createElement( 'div' );
593
- resizerTopRight.classList.add( 'resizer', 'top-right' );
594
- resizerTopRight.addEventListener( 'mousedown', e => {
595
- this.resizeClick( blockContent.getElementsByClassName( 'cdx-block' )[ 0 ], resizerTopRight, e );
596
- } );
597
-
598
-
599
- const resizerBottomLeft = document.createElement( 'div' );
600
- resizerBottomLeft.classList.add( 'resizer', 'bottom-left' );
601
- resizerBottomLeft.addEventListener( 'mousedown', e => {
602
- this.resizeClick( blockContent.getElementsByClassName( 'cdx-block' )[ 0 ], resizerBottomLeft, e );
603
- } );
604
-
605
- const resizerBottomRight = document.createElement( 'div' );
606
- resizerBottomRight.classList.add( 'resizer', 'bottom-right' );
607
- resizerBottomRight.addEventListener( 'mousedown', e => {
608
- this.resizeClick( blockContent.getElementsByClassName( 'cdx-block' )[ 0 ], resizerBottomRight, e );
609
- } );
610
-
611
- resizers.appendChild( resizerTopLeft );
612
- resizers.appendChild( resizerTopRight );
613
- resizers.appendChild( resizerBottomLeft );
614
- resizers.appendChild( resizerBottomRight );
615
- resizable.appendChild( resizers );
616
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].appendChild( resizable );
617
- }
618
-
619
-
620
-
621
- /**
622
- * click event to resize handles
623
- * preserve aspect ratio
624
- * prevent block from moving when dragging resize handles
625
- * max size = 100%
626
- * min size = 50px
627
- * @param {HTMLElement} blockContent - wrapper for block content
628
- * @public
629
- * @return {void}
630
- * */
631
- resizeClick( blockContent, handle, e ) {
632
-
633
- let startX = 0;
634
- let startY = 0;
635
- let startWidth = 0;
636
- let startHeight = 0;
637
-
638
- const mouseMoveHandler = e => {
639
-
640
- const dx = e.clientX - startX;
641
- const dy = e.clientY - startY;
642
-
643
- const newWidth = ( startWidth + dx );
644
-
645
- if ( newWidth > 50 ) {
646
- blockContent.style.width = newWidth + 'px';
647
- }
648
-
649
-
650
-
651
-
652
-
653
- };
654
-
655
- const mouseUpHandler = () => {
656
-
657
- let blockWidth = parseInt( document.defaultView.getComputedStyle( blockContent ).width, 10 );
658
-
659
- if ( blockWidth > 0 ) {
660
- this.data.resizeSize = blockWidth;
661
- }
662
-
663
- document.removeEventListener( 'mousemove', mouseMoveHandler );
664
- document.removeEventListener( 'mouseup', mouseUpHandler );
665
-
666
- this.block.dispatchChange();
667
-
668
- };
669
-
670
- document.addEventListener( 'mousemove', mouseMoveHandler );
671
- document.addEventListener( 'mouseup', mouseUpHandler );
672
-
673
- startX = e.clientX;
674
- startY = e.clientY;
675
-
676
- startWidth = parseInt( document.defaultView.getComputedStyle( blockContent ).width, 10 );
677
-
678
- startHeight = parseInt( document.defaultView.getComputedStyle( blockContent ).height, 10 );
679
-
680
-
681
- }
682
-
683
-
684
- /**
685
- * Remove resize handles from block
686
- * @param {HTMLElement} blockContent - wrapper for block content
687
- * @public
688
- * @return {void}
689
- */
690
- unresize( blockContent ) {
691
- const unresizable = blockContent.getElementsByClassName( 'resizable' )[ 0 ];
692
- if ( unresizable ) {
693
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].removeChild( unresizable );
694
- }
695
-
696
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.width = 'auto';
697
-
698
-
699
- }
700
-
701
-
702
-
703
-
704
- /**
705
- * Remove tunes from block wrapper
706
- * @param {HTMLElement} blockContent - wrapper for block content
707
- * @public
708
- * @return {HTMLElement}
709
- */
710
- unwrap( blockContent ) {
711
- console.log( 'unwrap' );
712
- //remove tunes from block
713
- this.buttons.forEach( button => {
714
- button.classList.remove( this.CSS.buttonActive );
715
- } );
716
-
717
- //remove isFloatLeft class
718
- blockContent.classList.remove( this.CSS.isFloatLeft );
719
-
720
- //remove isFloatRight class
721
- blockContent.classList.remove( this.CSS.isFloatRight );
722
-
723
- //remove isCenter class
724
- blockContent.classList.remove( this.CSS.isCenter );
725
-
726
- //remove isSizeSmall class
727
- blockContent.classList.remove( this.CSS.isSizeSmall );
728
-
729
- //remove isSizeMiddle class
730
- blockContent.classList.remove( this.CSS.isSizeMiddle );
731
-
732
- //remove isSizeLarge class
733
- blockContent.classList.remove( this.CSS.isSizeLarge );
734
-
735
- //remove isResize class
736
- blockContent.classList.remove( this.CSS.isResize );
737
-
738
- //remove isCrop class
739
- blockContent.classList.remove( this.CSS.isCrop );
740
-
741
- //remove isCropped class
742
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].classList.remove( 'isCropped' );
743
-
744
- //remove isCropping class
745
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].classList.remove( 'isCropping' );
746
-
747
- //remove min and max width
748
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.minWidth = '';
749
- blockContent.getElementsByClassName( 'cdx-block' )[ 0 ].style.maxWidth = '';
750
-
751
- //remove image width and height
752
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.width = '';
753
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].style.height = '';
754
-
755
- //remove image left and top
756
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.left = '';
757
-
758
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.top = '';
759
-
760
- //remove image width and height
761
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.width = '';
762
-
763
- blockContent.getElementsByClassName( 'image-tool__image' )[ 0 ].getElementsByTagName( 'img' )[ 0 ].style.height = '';
764
-
765
- //remove resize handles
766
- this.unresize( blockContent );
767
-
768
- //remove crop handles
769
- this.uncrop( blockContent );
770
-
771
- //remove cropper interface
772
- if ( this.cropperInterface ) {
773
- this.cropperInterface.destroy();
774
- }
775
-
776
- //remove crop data
777
- this.data.cropperFrameHeight = 0;
778
- this.data.cropperFrameWidth = 0;
779
- this.data.cropperFrameLeft = 0;
780
- this.data.cropperFrameTop = 0;
781
- this.data.cropperImageHeight = 0;
782
- this.data.cropperImageWidth = 0;
783
-
784
- return blockContent;
785
- }
786
-
787
-
788
-
789
- /**
790
- * Add tune to block data
791
- * @private
792
- * @return {void}
793
- * */
794
- save() {
795
- return this.data;
796
- }
797
-
798
- /**
799
- * Append tunes to block wrapper
800
- * @param {HTMLElement} blockContent - wrapper for block content
801
- * @public
802
- * @return {HTMLElement}
803
- * */
804
- wrap( blockContent ) {
805
-
806
- //createview if not exists
807
- if ( !this.wrapper ) {
808
- this.wrapper = this.createView();
809
- }
810
-
811
-
812
- this.apply( blockContent );
813
-
814
-
815
-
816
-
817
- return blockContent;
818
- }
819
-
820
-
821
- /**
822
- * Creates a view for tunes
823
- * @return {HTMLElement}
824
- * @private
825
- * */
826
- createView() {
827
- this.buttons = this.settings.map( tune => {
828
- const el = document.createElement( 'div' );
829
- const buttonIco = document.createElement( 'span' );
830
- const buttonTxt = document.createElement( 'span' );
831
- el.classList.add( this.CSS.button );
832
- buttonTxt.style.fontSize = '8px';
833
- buttonIco.innerHTML = tune.icon;
834
- buttonTxt.innerHTML = tune.label;
835
- el.appendChild( buttonIco );
836
- el.appendChild( buttonTxt );
837
- el.dataset.tune = tune.name;
838
- el.title = tune.label;
839
-
840
- el.addEventListener( 'click', e => this.tuneClicked( e, el ) );
841
-
842
- return el;
843
- } );
844
- const wrapper = document.createElement( 'div' );
845
- this.buttons.forEach( button => {
846
- wrapper.appendChild
847
- ( button );
848
- } );
849
- wrapper.classList.add( this.CSS.wrapper );
850
- return wrapper;
851
-
852
- }
853
-
854
- /**
855
- * Checks if tune is active
856
- * @param {string} tune - tune name
857
- * @return {boolean}
858
- * @private
859
- * */
860
- isTuneActive( tune ) {
861
- return !!this.data[ tune ];
862
- }
863
-
864
- /**
865
- * Makes buttons with tunes
866
- * @return {HTMLElement}
867
- * @public
868
- * */
869
- render() {
870
- //when editor is ready
871
- this.buttons.forEach( button => {
872
- button.classList.toggle( this.CSS.buttonActive, this.isTuneActive( button.dataset.tune ) );
873
- }
874
- );
875
-
876
- return this.view;
877
- }
878
-
879
-
880
- /**
881
- * Destroys the plugin
882
- * @public
883
- * @return {void}
884
- * */
885
- destroy() {
886
- this.wrapper = null;
887
- this.buttons = null;
888
- }
889
-
890
-
891
- }
892
-
893
-