Newer
Older
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
<template>
<div class="cw-block cw-block-canvas" ref="block">
<courseware-default-block
:block="block"
:canEdit="canEdit"
:isTeacher="isTeacher"
:preview="true"
@storeEdit="storeBlock"
@closeEdit="initCurrentData"
>
<template #content>
<div v-if="currentTitle" class="cw-block-title">
{{ currentTitle }}
</div>
<div class="cw-canvasblock-toolbar">
<div class="cw-canvasblock-buttonset">
<button class="cw-canvasblock-reset" :title="$gettext('Zurücksetzen')" @click="reset"></button>
<button class="cw-canvasblock-undo" :title="$gettext('Rückgängig')" @click="undo"></button>
<button v-if="hasUploadFolder" class="cw-canvasblock-store" :title="$gettext('Bild im Dateibereich speichern')" @click="store"></button>
</div>
<div class="cw-canvasblock-buttonset">
<button
v-for="(rgba, color) in colors"
:key="color"
class="cw-canvasblock-color"
:class="[currentColor === color ? 'selected-color' : '', color]"
@click="setColor(color)"
/>
</div>
<div class="cw-canvasblock-buttonset">
<button
class="cw-canvasblock-size cw-canvasblock-size-small"
:class="{ 'selected-size': currentSize === 2 }"
:title="$gettext('klein')"
@click="setSize('small')"
/>
<button
class="cw-canvasblock-size cw-canvasblock-size-normal"
:class="{ 'selected-size': currentSize === 5 }"
:title="$gettext('normal')"
@click="setSize('normal')"
/>
<button
class="cw-canvasblock-size cw-canvasblock-size-large"
:class="{ 'selected-size': currentSize === 8 }"
:title="$gettext('groß')"
@click="setSize('large')"
/>
<button
class="cw-canvasblock-size cw-canvasblock-size-huge"
:class="{ 'selected-size': currentSize === 12 }"
:title="$gettext('riesig')"
@click="setSize('huge')"
/>
</div>
<div class="cw-canvasblock-buttonset">
<button
class="cw-canvasblock-tool cw-canvasblock-tool-pen"
:class="{ 'selected-tool': currentTool === 'pen' }"
:title="$gettext('Zeichenwerkzeug')"
@click="setTool('pen')"
/>
<button
class="cw-canvasblock-tool cw-canvasblock-tool-text"
:class="{ 'selected-tool': currentTool === 'text' }"
:title="$gettext('Textwerkzeug')"
@click="setTool('text')"
>
T
</button>
</div>
</div>
<img :src="currentUrl" class="cw-canvasblock-original-img" ref="image" @load="buildCanvas" />
<input
v-show="textInput"
class="cw-canvasblock-text-input"
ref="textInputField"
@keyup="textInputKeyUp"
/>
<canvas
class="cw-canvasblock-canvas"
:class="{
'cw-canvasblock-tool-selected-pen': currentTool === 'pen',
'cw-canvasblock-tool-selected-text': currentTool === 'text',
}"
ref="canvas"
@mousedown="mouseDown"
@mousemove="mouseMove"
@mouseup="mouseUp"
@mouseout="mouseUp"
@mouseleave="mouseUp"
/>
<div class="cw-canvasblock-hints">
<div v-show="write" class="messagebox messagebox_info cw-canvasblock-text-info">
<translate>Texteingabe mit Enter-Taste bestätigen</translate>
</div>
</div>
</template>
<template v-if="canEdit" #edit>
<form class="default" @submit.prevent="">
<label>
<translate>Überschrift</translate>
<input type="text" v-model="currentTitle" />
</label>
<label>
<translate>Hintergrundbild</translate>
<select v-model="currentImage">
<option value="true"><translate>Ja</translate></option>
<option value="false"><translate>Nein</translate></option>
</select>
</label>
<label v-if="currentImage === 'true'">
<translate>Bilddatei</translate>
<courseware-file-chooser
v-model="currentFileId"
:isImage="true"
@selectFile="updateCurrentFile"
/>
</label>
<label>
<translate>Speicherort</translate>
<courseware-folder-chooser v-model="currentUploadFolderId" :unchoose="true"/>
</label>
<label>
<translate>Werte anderer Nutzer anzeigen</translate>
<select v-model="currentShowUserData">
<option value="off"><translate>deaktiviert</translate></option>
<option value="teacher"><translate>nur für Lehrende</translate></option>
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<option value="all"><translate>für alle</translate></option>
</select>
</label>
</form>
</template>
<template #info>
<p><translate>Informationen zum Leinwand-Block</translate></p>
</template>
</courseware-default-block>
</div>
</template>
<script>
import CoursewareDefaultBlock from './CoursewareDefaultBlock.vue';
import CoursewareFileChooser from './CoursewareFileChooser.vue';
import CoursewareFolderChooser from './CoursewareFolderChooser.vue';
import { mapActions, mapGetters } from 'vuex';
export default {
name: 'courseware-canvas-block',
components: {
CoursewareDefaultBlock,
CoursewareFileChooser,
CoursewareFolderChooser,
},
props: {
block: Object,
canEdit: Boolean,
isTeacher: Boolean,
},
data() {
return {
currentTitle: '',
currentImage: '',
currentFileId: '',
currentUploadFolderId: '',
currentShowUserData: '',
currentFile: {},
context: {},
paint: false,
write: false,
clickX: [],
clickY: [],
clickDrag: [],
clickColor: [],
colors: {
white: 'rgba(255,255,255,1)',
blue: 'rgba(52,152,219,1)',
green: 'rgba(46,204,113,1)',
purple: 'rgba(155,89,182,1)',
red: 'rgba(231,76,60,1)',
yellow: 'rgba(254,211,48,1)',
orange: 'rgba(243,156,18,1)',
grey: 'rgba(149,165,166,1)',
darkgrey: 'rgba(52,73,94,1)',
black: 'rgba(0,0,0,1)',
},
currentColor: '',
currentColorRGBA: '',
sizes: { small: 2, normal: 5, large: 8, huge: 12 },
clickSize: [],
currentSize: '',
tools: { pen: 'pen', text: 'text' },
currentTool: '',
clickTool: [],
Text: [],
textInput: false,
file: null
};
},
computed: {
...mapGetters({
userId: 'userId',
getUserDataById: 'courseware-user-data-fields/byId',
usersById: 'users/byId',
}),
userData() {
return this.getUserDataById({ id: this.block.relationships['user-data-field'].data.id });
},
canvasDraw() {
if (this.userData !== undefined && this.userData.attributes.payload.canvas_draw) {
return this.userData.attributes.payload.canvas_draw;
} else {
return false;
}
},
title() {
return this.block?.attributes?.payload?.title;
},
image() {
return this.block?.attributes?.payload?.image;
},
fileId() {
return this.block?.attributes?.payload?.file_id;
},
uploadFolderId() {
return this.block?.attributes?.payload?.upload_folder_id;
},
showUsersData() {
return this.block?.attributes?.payload?.show_usersdata;
},
currentUrl() {
if (this.currentFile?.meta) {
return this.currentFile.meta['download-url'];
} else if(this.currentFile?.download_url) {
return this.currentFile.download_url;
} else {
return '';
}
},
currentFileName() {
if (this.currentFile?.attributes?.name) {
return this.currentFile.attributes.name;
} else {
return this.currentTitle + '.jpg';
}
},
hasUploadFolder() {
return this.currentUploadFolderId !== "";
},
},
mounted() {
this.loadFileRefs(this.block.id).then((response) => {
this.file = response[0];
this.currentFile = this.file;
this.initCurrentData();
this.buildCanvas();
});
},
methods: {
...mapActions({
updateBlock: 'updateBlockInContainer',
loadFileRefs: 'loadFileRefs',
createFile: 'createFile',
companionSuccess: 'companionSuccess',
companionError: 'companionError',
}),
initCurrentData() {
this.currentTitle = this.title;
this.currentImage = this.image;
this.currentFileId = this.fileId;
this.currentUploadFolderId = this.uploadFolderId;
this.currentShowUserData = this.showUsersData;
if (this.canvasDraw) {
this.clickX = JSON.parse(this.canvasDraw.clickX);
this.clickY = JSON.parse(this.canvasDraw.clickY);
this.clickDrag = JSON.parse(this.canvasDraw.clickDrag);
this.clickColor = JSON.parse(this.canvasDraw.clickColor);
this.clickSize = JSON.parse(this.canvasDraw.clickSize);
this.clickTool = JSON.parse(this.canvasDraw.clickTool);
this.Text = JSON.parse(this.canvasDraw.Text);
}
},
updateCurrentFile(file) {
this.currentFile = file;
this.currentFileId = file.id;
this.buildCanvas();
},
setColor(color) {
if (this.write) {
return;
}
this.currentColor = color;
this.currentColorRGBA = this.colors[color];
},
setSize(size) {
if (this.textInput) {
return;
}
this.currentSize = this.sizes[size];
},
setTool(tool) {
if (this.write) {
this.clickX.pop();
this.clickY.pop();
this.clickDrag.pop();
this.clickColor.pop();
this.clickSize.pop();
this.clickTool.pop();
this.write = false;
this.textInput = false;
}
this.currentTool = this.tools[tool];
},
reset() {
this.clickX.length = 0;
this.clickY.length = 0;
this.clickDrag.length = 0;
this.clickColor.length = 0;
this.clickSize.length = 0;
this.clickTool.length = 0;
this.Text.length = 0;
this.paint = false;
this.write = false;
this.textInput = false;
this.redraw();
},
buildCanvas() {
let blockElem = this.$refs.block;
let image = this.$refs.image;
let canvas = this.$refs.canvas;
canvas.width = blockElem.offsetWidth - 2;
if (this.currentImage === 'true' && image.height > 0) {
canvas.height = Math.round((canvas.width / image.width) * image.height);
} else {
canvas.height = 500;
}
this.context = canvas.getContext('2d');
this.currentColor = 'blue';
this.currentColorRGBA = this.colors['blue'];
this.currentSize = this.sizes['normal'];
this.currentTool = this.tools['pen'];
this.redraw();
},
redraw() {
let view = this;
let context = view.context;
let clickX = view.clickX;
let clickY = view.clickY;
context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
context.fillStyle = '#ffffff';
context.fillRect(0, 0, context.canvas.width, context.canvas.height); // set background
if (view.currentImage === 'true') {
let outlineImage = new Image();
outlineImage.src = this.currentUrl;
context.drawImage(outlineImage, 0, 0, context.canvas.width, context.canvas.height);
}
context.lineJoin = 'round';
for (var i = 0; i < clickX.length; i++) {
if (view.clickTool[i] === 'pen') {
context.beginPath();
if (view.clickDrag[i] && i) {
context.moveTo(clickX[i - 1], clickY[i - 1]);
} else {
context.moveTo(clickX[i] - 1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.strokeStyle = view.clickColor[i];
context.lineWidth = view.clickSize[i];
context.stroke();
}
if (view.clickTool[i] === 'text') {
let fontsize = view.clickSize[i] * 6;
context.font = fontsize + 'px Arial ';
context.fillStyle = view.clickColor[i];
context.fillText(view.Text[i], clickX[i], clickY[i] + fontsize);
}
}
},
mouseDown(e) {
if (this.write) {
let view = this;
this.$refs.textInputField.focus();
window.setTimeout(function () {
view.$refs.textInputField.focus();
}, 0);
return;
}
if (this.currentTool === 'pen') {
this.paint = true;
this.addClick(e.offsetX, e.offsetY, false);
this.redraw();
}
if (this.currentTool === 'text') {
this.write = true;
this.addClick(e.offsetX, e.offsetY, false);
}
},
mouseMove(e) {
if (this.paint) {
this.addClick(e.offsetX, e.offsetY, true);
this.redraw();
}
},
mouseUp(e) {
this.storeDraw();
this.paint = false;
},
addClick(x, y, dragging) {
this.clickX.push(x);
this.clickY.push(y);
this.clickDrag.push(dragging);
this.clickColor.push(this.currentColorRGBA);
this.clickSize.push(this.currentSize);
this.clickTool.push(this.currentTool);
if (this.currentTool === 'text') {
this.enableTextInput(x, y);
} else {
this.Text.push('');
}
},
undo() {
let dragging = this.clickDrag[this.clickDrag.length - 1];
this.clickX.pop();
this.clickY.pop();
this.clickDrag.pop();
this.clickColor.pop();
this.clickSize.pop();
this.clickTool.pop();
if (this.write) {
this.textInput = false;
this.write = false;
} else {
this.Text.pop('');
}
if (dragging) {
this.undo();
}
this.redraw();
},
enableTextInput(x, y) {
let view = this;
let fontsize = this.currentSize * 6;
this.textInput = true;
let input = this.$refs.textInputField;
input.value = '';
input.style.position = 'absolute';
input.style.top = this.$refs.canvas.offsetTop + y + 'px';
input.style.left = 320 + x + 'px';
input.style.lineHeight = fontsize + 'px';
input.style.fontSize = fontsize + 'px';
input.style.width = '300px';
window.setTimeout(function () {
view.$refs.textInputField.focus();
}, 0);
},
textInputKeyUp(e) {
if (e.defaultPrevented) {
return;
}
let key = e.key || e.keyCode;
if (key === 'Enter' || key === 13) {
this.Text.push(this.$refs.textInputField.value);
this.textInput = false;
this.write = false;
this.redraw();
}
if (key === 'Escape' || key === 'Esc' || key === 27) {
this.clickX.pop();
this.clickY.pop();
this.clickDrag.pop();
this.clickColor.pop();
this.clickSize.pop();
this.clickTool.pop();
this.textInput = false;
this.write = false;
}
},
async storeDraw() {
let data = {};
data.type = 'courseware-user-data-fields';
data.id = this.block.relationships['user-data-field'].data.id;
data.relationships = {};
data.relationships.block = {};
data.relationships.block.data = {};
data.relationships.block.data.id = this.block.id;
data.relationships.block.data.type = this.block.type;
data.attributes = {};
data.attributes.payload = {};
data.attributes.payload.canvas_draw = {};
data.attributes.payload.canvas_draw.clickX = JSON.stringify(this.clickX);
data.attributes.payload.canvas_draw.clickY = JSON.stringify(this.clickY);
data.attributes.payload.canvas_draw.clickDrag = JSON.stringify(this.clickDrag);
data.attributes.payload.canvas_draw.clickColor = JSON.stringify(this.clickColor);
data.attributes.payload.canvas_draw.clickSize = JSON.stringify(this.clickSize);
data.attributes.payload.canvas_draw.clickTool = JSON.stringify(this.clickTool);
data.attributes.payload.canvas_draw.Text = JSON.stringify(this.Text);
await this.$store.dispatch('courseware-user-data-fields/update', data);
},
storeBlock() {
let attributes = {};
attributes.payload = {};
attributes.payload.title = this.currentTitle;
attributes.payload.image = this.currentImage;
if (this.currentImage === 'true') {
attributes.payload.file_id = this.currentFileId;
} else {
attributes.payload.file_id = '';
}
attributes.payload.upload_folder_id = this.currentUploadFolderId;
attributes.payload.show_usersdata = this.currentShowUserData;
this.updateBlock({
attributes: attributes,
blockId: this.block.id,
containerId: this.block.relationships.container.data.id,
});
},
async store() {
let user = this.usersById({id: this.userId});
let imageBase64 = this.context.canvas.toDataURL("image/jpeg", 1.0);
let image = await fetch(imageBase64);
let imageBlob = await image.blob();
let file = {};
file.attributes = {};
if(this.currentImage === 'true') {
file.attributes.name = (user.attributes["formatted-name"]).replace(/\s+/g, '_') + '_' + this.currentFile.attributes.name;
} else {
file.attributes.name = (user.attributes["formatted-name"]).replace(/\s+/g, '_') + '_' + this.block.attributes.title + '_' + this.block.id;
}
let img = false;
try {
img = await this.createFile({
file: file,
filedata: imageBlob,
folder: {id: this.currentUploadFolderId}
});
}
catch(e) {
this.companionError({
info: this.$gettext('Es ist ein Fehler aufgetretten! Das Bild konnte nicht gespeichert werden.')
});
console.log(e);
}
if(img && img.type === 'file-refs') {
this.companionSuccess({
info: this.$gettext('Bild wurde erfolgreich im Dateibereich abgelegt.')
});
}
},
},
};
</script>