This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
108 lines (92 loc) · 3.17 KB
/
main.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
import './style.css'
import Konva from 'konva';
const stage = new Konva.Stage({
container: 'app',
width: innerWidth,
height: innerHeight,
});
const layer = new Konva.Layer();
const simpleText = new Konva.Text({
x: innerWidth / 16,
y: innerHeight / 12,
text: '', /* <= Later I could make it as [non-printable], but blink-able char */
fontSize: 32,
fontFamily: 'Roboto',
fill: 'black',
});
const textNodeDetection = new Konva.Transformer({
nodes: [simpleText],
width: (simpleText.text().length * simpleText.fontSize()),
borderStroke: 'red',
rotateEnabled: false,
/* empty array disables all bounding-box anchors as follows : */
enabledAnchors: [/* 'middle-left', 'middle-right' */],
});
textNodeDetection.on('transform', function(){
/* console.log(this.width(), simpleText.width(), simpleText.text().length * simpleText.fontSize()); */
this.setAttrs({
width: Math.max(simpleText.width() * simpleText.scaleX(), simpleText.text().length * simpleText.fontSize()),
scaleX: 1,
scaleY: 1,
});
})
// add the shapes to the layer
layer.add(...[/* textNodeDetection, */simpleText]);
// add the layer to the stage
stage.add(layer);
/* === Rich text editing right inside canvas (Konva.js) === */
const canvas = layer.getCanvas()._canvas;
stage.content.addEventListener("click", async function(){
await document.documentElement.requestFullscreen();
/* document.exitFullscreen() */
canvas.tabIndex = 1;
canvas.focus();
canvas.style.outline = "none";
// DEV_NOTE # text_cursor (ibeam) work very buggy, better find a way to keep blinking a wee cursor-like shape that follows along
// let counter = 0;
/* let text_cursor = new RegExp('\u{1D100}').source; */
// setInterval(()=>{
// if(counter % 2){
// simpleText.setAttr("text", simpleText.text().replace(text_cursor, ""))
// }
// else {
// simpleText.setAttr("text", simpleText.text() + text_cursor)
// }
// counter++;
// }, 750)
})
canvas.addEventListener("keydown", async function(e){
// DEV_NOTE # let's emulate event via keydown action rather than action of resizing the textNodeDetection boundingBox
textNodeDetection.dispatchEvent(
new CustomEvent('transform')
)
let currentText = simpleText.getAttr("text");
switch (e.code){
case 'Backspace':
simpleText.setAttr("text", currentText.substring(0,currentText.length-1));
break;
case 'Delete':
simpleText.setAttr("text", currentText.substring(0,-1));
break;
case 'Enter':
simpleText.setAttr("text", currentText + "\n");
break;
case 'AltLeft':
case 'ControlLeft':
case 'ShiftLeft':
case 'CapsLock':
// # possible some other cases that results to e.code ...
e.preventDefault()
break;
case 'Tab':
e.preventDefault()
simpleText.setAttr("text", simpleText.getAttr("text") + "\t\t\t\t");
break;
default:
console.log(e.code)
/** {@link https://www.w3.org/TR/uievents-code/#key-alphanumeric-writing-system}
* - opt in the rest of writing system keys as is
* */
simpleText.setAttr("text", simpleText.getAttr("text") + e.key);
}
})