-
Notifications
You must be signed in to change notification settings - Fork 342
/
resize-handler.ts
140 lines (113 loc) · 2.94 KB
/
resize-handler.ts
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
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* [[include:plugins/resize-handler/README.md]]
* @packageDocumentation
* @module plugins/resize-handler
*/
import type { IJodit, IPointBound } from 'jodit/types';
import { autobind } from 'jodit/core/decorators';
import { Dom } from 'jodit/core/dom';
import { pluginSystem } from 'jodit/core/global';
import { Plugin } from 'jodit/core/plugin';
import { Icon } from 'jodit/core/ui';
import './config';
@autobind
export class resizeHandler extends Plugin {
/** @override **/
static override requires: string[] = ['size'];
/** @override **/
protected afterInit(editor: IJodit): void {
const { height, width, allowResizeX } = editor.o;
let { allowResizeY } = editor.o;
if (height === 'auto' && width !== 'auto') {
allowResizeY = false;
}
if (
(height !== 'auto' || width !== 'auto') &&
(allowResizeX || allowResizeY)
) {
editor.statusbar.setMod('resize-handle', true);
editor.e
.on('toggleFullSize.resizeHandler', () => {
this.handle.style.display = editor.isFullSize
? 'none'
: 'block';
})
.on(
this.handle,
'mousedown touchstart',
this.onHandleResizeStart
)
.on(editor.ow, 'mouseup touchend', this.onHandleResizeEnd);
editor.container.appendChild(this.handle);
}
}
/**
* Plugin in resize process
*/
private isResized: boolean = false;
/**
* Start point
*/
private start: IPointBound = {
x: 0,
y: 0,
w: 0,
h: 0
};
/**
* Handler: Click on handle - start resizing
*/
private onHandleResizeStart(e: MouseEvent): void {
this.isResized = true;
this.start.x = e.clientX;
this.start.y = e.clientY;
this.start.w = this.j.container.offsetWidth;
this.start.h = this.j.container.offsetHeight;
this.j.lock();
this.j.e.on(this.j.ow, 'mousemove touchmove', this.onHandleResize);
e.preventDefault();
}
/**
* Handler: Mouse move after start resizing
*/
private onHandleResize(e: MouseEvent): void {
if (!this.isResized) {
return;
}
if (this.j.o.allowResizeY) {
this.j.e.fire('setHeight', this.start.h + e.clientY - this.start.y);
}
if (this.j.o.allowResizeX) {
this.j.e.fire('setWidth', this.start.w + e.clientX - this.start.x);
}
this.j.e.fire('resize');
}
/**
* End of resizing
*/
private onHandleResizeEnd(): void {
if (this.isResized) {
this.isResized = false;
this.j.e.off(this.j.ow, 'mousemove touchmove', this.onHandleResize);
this.j.unlock();
}
}
/**
* Resize handle
*/
private handle = this.j.c.div(
'jodit-editor__resize',
Icon.get('resize_handler')
);
/** @override **/
protected beforeDestruct(): void {
Dom.safeRemove(this.handle);
this.j.e.off(this.j.ow, 'mouseup touchsend', this.onHandleResizeEnd);
}
}
pluginSystem.add('resizeHandler', resizeHandler);