summaryrefslogtreecommitdiffstats
path: root/builtin-programs/editor-control.folk
blob: a5f3f49183eb4b6762f72f8034080c09d4fcb66b (plain)
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
When /page/ has editor code /editorCode/ & /page/ has program code /programCode/ {
    Claim $page has base64 editor code [binary encode base64 $editorCode] \
                           program code [binary encode base64 $programCode]
}

Wish the web server handles route "/editor-control" with hidden true handler {
    html {
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Editor copy/paste</title>
        <script src="/lib/folk.js"></script>
    </head>
    <body>
        <span id="status">Status</span>
        <p>
            Select a keyboard: <select id="keyboard-select"></select>
        </p>
        <textarea id="code" cols="120" rows="40"></textarea>
        <script>
const ws = new FolkWS(document.getElementById('status'));
const keyboardSelect = document.querySelector("#keyboard-select");
const textarea = document.querySelector("#code");

var currentKeyboard = null;
var programCode = ""; // not the same as editor code
var cursorPosition = [0, 0];

// temporarily disable event processing after sending new code to prevent recursive event sends
var allowLocalEventsToProcess = true;
var allowRemoteEventsToProcess = true;
var _remoteTimoutHandle;
var _localTimeoutHandle;
function disableRemoteEventProcessing(durationMs) {
    if (_remoteTimoutHandle) clearTimeout(_remoteTimoutHandle);
    allowRemoteEventsToProcess = false;

    _remoteTimoutHandle = setTimeout(() => {
        allowRemoteEventsToProcess = true;
    }, durationMs);
}

function disableLocalEventProcessing(durationMs) {
    if (_localTimeoutHandle) clearTimeout(_localTimeoutHandle);
    allowLocalEventsToProcess = false;

    _localTimeoutHandle = setTimeout(() => {
        allowLocalEventsToProcess = true;
    }, durationMs);
}

function updateProgramCode() {
    disableRemoteEventProcessing(500);

    const { page, kbPath } = currentKeyboard;

    const currentCode = textarea.value;
    programCode = currentCode;

    const id = page + kbPath;
    ws.run(tcl`
        Hold (non-capturing) (on builtin-programs/editor.folk) ${"cursor" + kbPath} {
            Claim the ${kbPath} cursor is [list ${cursorPosition[0]} ${cursorPosition[1]}]
            Hold (on builtin-programs/editor.folk) ${"code" + kbPath} {
                Claim ${id} has program code [binary decode base64 ${btoa(currentCode)}]
                Claim ${id} has editor code [binary decode base64 ${btoa(currentCode)}]
            }
        }
    `);
}

function updateCursorAndCode(ev) {
    if (!allowLocalEventsToProcess) return;
    disableRemoteEventProcessing(500);

    const { page, kbPath } = currentKeyboard;

    const newCode = ev.target.value;

    // figure out cursor position
    const currentPosition = textarea.selectionStart;
    const linesBefore = newCode.substring(0, currentPosition).split("\n");
    const y = linesBefore.length - 1;
    const x = linesBefore[linesBefore.length - 1].length;

    cursorPosition = [x, y];

    const id = page + kbPath;
    ws.run(tcl`
        Hold (non-capturing) (on builtin-programs/editor.folk) ${"cursor" + kbPath} {
            Claim the ${kbPath} cursor is [list ${x} ${y}]
            Hold (on builtin-programs/editor.folk) ${"code" + kbPath} {
                Claim ${id} has program code [binary decode base64 ${btoa(programCode)}]
                Claim ${id} has editor code [binary decode base64 ${btoa(newCode)}]
            }
        }
    `);
}

textarea.addEventListener("input", updateCursorAndCode);
textarea.addEventListener("selectionchange", updateCursorAndCode);
textarea.addEventListener("keydown", ev => {
    if(ev.keyCode === 83 /* s */ && (navigator.platform.match("Mac") ? ev.metaKey : ev.ctrlKey)) {
        ev.preventDefault();
        updateProgramCode();
    }
});

var lastKeyboard; // to clean up the previous keyboard when another is picked
async function selectKeyboard({ page, kbPath }) {
    if (lastKeyboard) lastKeyboard.stop();

    currentKeyboard = { page, kbPath };

    const id = page + kbPath;
    lastKeyboard = await ws.watch(`${id} has base64 editor code /editorCode/ program code /programCode/ & the ${kbPath} cursor is /cursor/`, {
        add: ({ editorCode, programCode: _programCode, cursor }) => {
            if (!allowRemoteEventsToProcess) return;
            disableLocalEventProcessing(500);

            programCode = atob(_programCode);

            editorCode = atob(editorCode);
            textarea.value = editorCode;

            // figure out where the cursor is
            let [x, y] = loadList(cursor);
            x = parseInt(x); y = parseInt(y);

            cursorPosition = [x, y];

            const lines = editorCode.split("\n");

            let pos = 0;
            for (let i = 0; i < y; i++) {
                pos += lines[i].length + 1; // + 1 for newline
            }
            pos += x;

            textarea.focus();
            textarea.selectionStart = pos;
            textarea.selectionEnd = pos;
        }
    });
}

// update keyboard list as it changes
ws.watchCollected("/page/ is an editor & /page/ is a keyboard with path /kbPath/", keyboards => {
    keyboardSelect.innerHTML = "";

    for (let keyboard of keyboards) {
        let {page, kbPath} = keyboard;
        keyboardSelect.innerHTML += `<option value="${JSON.stringify(keyboard)}">${page} (${kbPath})</option>`;
    }

    if (keyboards.length === 1) {
        selectKeyboard(keyboards[0]);
    }
});

// fired when selected keyboard changes
keyboardSelect.addEventListener("input", (ev) => {
    selectKeyboard(JSON.parse(ev.target.value));
});
        </script>
    </body>
</html>
    }
}