-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIHandler.js
280 lines (243 loc) · 10.2 KB
/
UIHandler.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { stopAnimation, startAnimation, isPlaying, } from "./drawingHandler.js";
import { startDrawing, stopDrawing, doDrawing, redrawPaths, setPressureMinMax } from './drawingHandler.js';
import { getDrawingMode, setDrawingMode, DRAWING, ERASING, TYPING, READING, NUMBER_OF_DRAWING_MODE } from "./drawingHandler.js";
import { paths, currentPageNum, pdfDoc, changePage, handleFileSelect } from "./pdfHandler.js";
const pdfSVG = document.getElementById('pdf-svg');
const playSlider = document.getElementById('playSlider');
export function updateSlider(_value, _max) {
playSlider.max = _max;
playSlider.value = _value;
}
export function updateButtons() {
// 次のページ、前のページボタンの有効/無効を設定
document.getElementById('prev-page').disabled = (currentPageNum <= 1);
if(pdfDoc){
document.getElementById('next-page').disabled = (currentPageNum >= pdfDoc.numPages);
} else {
document.getElementById('next-page').disabled = true;
}
document.getElementById('show-thumbnails').disabled = false;
document.getElementById('drawing-mode').disabled = false;
document.getElementById('undo').disabled = !(paths[currentPageNum] != null && paths[currentPageNum].length > 0);
// 手書きありページ一覧ボタンの有効/無効を設定
let hasHandwriting = Object.keys(paths).some(key => paths[key].length > 0);
document.getElementById('filter-pages').disabled = !hasHandwriting;
document.getElementById('share').disabled = !hasHandwriting;
}
export function toggleDrawingMode() {
setDrawingMode((getDrawingMode() + 1) % NUMBER_OF_DRAWING_MODE);
changeInteractionMode(getDrawingMode());
if(getDrawingMode() === DRAWING){
document.getElementById('drawing-mode').innerText = "[描く] 書く 消す 閲覧";
document.getElementById('drawing-mode').style.background = "#ffcccc";
} else if(getDrawingMode() === TYPING){
document.getElementById('drawing-mode').innerText = "描く [書く] 消す 閲覧";
document.getElementById('drawing-mode').style.background = "#ff cc";
} else if(getDrawingMode() === ERASING){
document.getElementById('drawing-mode').innerText = "描く 書く [消す] 閲覧";
document.getElementById('drawing-mode').style.background = "#ccccff";
if(isPlaying){
stopAnimation();
playSlider.value = playSlider.max;
redrawPaths(playSlider.max);
}
} else if(getDrawingMode() === READING){
document.getElementById('drawing-mode').style.background = "#ffffcc";
document.getElementById('drawing-mode').innerText = "描く 書く 消す [閲覧]";
}
}
export function changeInteractionMode(_mode){
if(_mode === READING){
pdfSVG.removeEventListener('touchstart', startDrawing, { passive: false });
pdfSVG.removeEventListener('touchend', stopDrawing, { passive: false });
pdfSVG.removeEventListener('touchmove', doDrawing, { passive: false });
pdfSVG.addEventListener('touchstart', handleTouchStart, false);
pdfSVG.addEventListener('touchmove', handleTouchMove, false);
pdfSVG.addEventListener('touchend', handleTouchEnd, false);
pdfSVG.addEventListener('pointerdown', handleTouchStart, false);
pdfSVG.addEventListener('pointermove', handleTouchMove, false);
pdfSVG.addEventListener('pointerup', handleTouchEnd, false);
pdfSVG.removeEventListener('mousedown', startDrawing);
pdfSVG.removeEventListener('mouseup', stopDrawing);
pdfSVG.removeEventListener('mousemove', doDrawing);
} else {
pdfSVG.removeEventListener('touchstart', handleTouchStart, false);
pdfSVG.removeEventListener('touchmove', handleTouchMove, false);
pdfSVG.removeEventListener('touchend', handleTouchEnd, false);
pdfSVG.addEventListener('touchstart', startDrawing, { passive: false });
pdfSVG.addEventListener('touchend', stopDrawing, { passive: false });
pdfSVG.addEventListener('touchmove', doDrawing, { passive: false });
pdfSVG.removeEventListener('pointerdown', handleTouchStart, false);
pdfSVG.removeEventListener('pointermove', handleTouchMove, false);
pdfSVG.removeEventListener('pointerup', handleTouchEnd, false);
pdfSVG.addEventListener('mousedown', startDrawing);
pdfSVG.addEventListener('mouseup', stopDrawing);
pdfSVG.addEventListener('mousemove', doDrawing);
}
}
export function handlePlayPause() {
if (isPlaying) {
stopAnimation();
} else {
startAnimation();
}
}
export function getScaleFromSlider(){
return 1 / parseFloat(document.getElementById('zoomSlider').value);
}
let xDown, yDown, initialDistance, lastTouchEnd;
function handleTouchStart(event) {
if (event.touches != null && event.touches.length != null){
if(event.touches.length === 2) {
initialDistance = getDistance(event.touches[0], event.touches[1]);
} else if (event.touches.length === 1) {
const firstTouch = event.touches[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
}
} else {
xDown = event.clientX;
yDown = event.clientY;
}
}
const thresholdSwipe = 50;
function handleTouchMove(event) {
if (event.touches != null && event.touches.length != null){
if (event.touches.length === 2) {
const currentDistance = getDistance(event.touches[0], event.touches[1]);
if (initialDistance) {
if (currentDistance > initialDistance) {
console.log('Pinch Out');
} else {
console.log('Pinch In');
}
}
initialDistance = currentDistance;
} else if (event.touches.length === 1) {
if (!xDown || !yDown) {
return;
}
const xUp = event.touches[0].clientX;
const yUp = event.touches[0].clientY;
const xDiff = xDown - xUp;
const yDiff = yDown - yUp;
if (Math.abs(xDiff) > Math.abs(yDiff)) {
if (xDiff > thresholdSwipe) {
console.log('Swipe Left');
changePage(1);
} else if(xDiff < -thresholdSwipe){
console.log('Swipe Right');
changePage(-1);
}
} else {
if (yDiff > thresholdSwipe) {
console.log('Swipe Up');
} else if(yDiff < -thresholdSwipe){
console.log('Swipe Down');
}
}
xDown = null;
yDown = null;
}
} else {
if (!xDown || !yDown) {
return;
}
const xUp = event.clientX;
const yUp = event.clientY;
const xDiff = xDown - xUp;
const yDiff = yDown - yUp;
if (Math.abs(xDiff) > Math.abs(yDiff)) {
console.log("swipe?" + xDiff);
console.log("(" + xDown + ", " + yDown + ") -> (" + xUp + ", " + yUp + ")");
if (xDiff > thresholdSwipe) {
console.log('Swipe Left');
changePage(1);
xDown = null;
yDown = null;
} else if(xDiff < -thresholdSwipe){
console.log('Swipe Right');
changePage(-1);
xDown = null;
yDown = null;
}
} else {
if (yDiff > thresholdSwipe) {
console.log('Swipe Up');
} else if(yDiff < -thresholdSwipe){
console.log('Swipe Down');
}
xDown = null;
yDown = null;
}
}
}
function handleTouchEnd(event) {
const now = new Date().getTime();
if (now - lastTouchEnd <= 300) {
event.preventDefault();
}
lastTouchEnd = now;
initialDistance = null;
xDown = null;
yDown = null;
}
function getDistance(touch1, touch2) {
const dx = touch1.clientX - touch2.clientX;
const dy = touch1.clientY - touch2.clientY;
return Math.sqrt(dx * dx + dy * dy);
}
export function initPressureRangeSlider(_defaultPressureMin, _defaultPressureMax){
const pressureRangeSlider = document.getElementById('pressure-range-slider');
noUiSlider.create(pressureRangeSlider, {
start: [1-_defaultPressureMax, 1-_defaultPressureMin],
connect: true,
orientation: 'vertical',
range: {
'min': 0,
'max': 1
},
step: 0.01
});
pressureRangeSlider.noUiSlider.on('update', (values) => {
setPressureMinMax(1-parseFloat(values[1]), 1-parseFloat(values[0]));
redrawPaths(parseInt(playSlider.value));
});
}
export function initDragAndDropUI(){
const pdfContainer = document.getElementById('pdf-container');
pdfContainer.style.border = '2px dashed #000';
pdfContainer.setAttribute('width', '100%');
pdfContainer.setAttribute('height', '100%');
pdfContainer.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
pdfContainer.style.border = '2px dashed #000';
});
pdfContainer.addEventListener('dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
pdfContainer.style.border = '1px solid #000';
});
pdfContainer.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
pdfContainer.style.border = '1px solid #000';
const files = e.dataTransfer.files;
if (files.length > 0) {
handleFileSelect({ target: { files: files } });
}
});
// ファイル選択ボタン機能
document.getElementById('file-select-button').addEventListener('click', () => {
document.getElementById('file-input').click();
});
document.getElementById('file-input').addEventListener('change', handleFileSelect);
// requestAnimationFrame(() => {
// const body = document.getElementById('bodyContent');
// const rect = body.getBoundingClientRect();
// console.log(rect);
// pdfContainer.setAttribute('width', rect.width);
// pdfContainer.setAttribute('height', rect.height);
// });
}