1/*  DokuWiki MoaiEditor Scroll_to.js file
2    Version : 0.5b (2026-05-08)
3    Author  : MoaiTools <info@moaitools.org>
4    License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */
5
6/*
7*/
8MoaiEditor.WatchSelection = class {
9
10    constructor(outer) {
11
12        // Arguments
13        this.outer = outer;
14
15        // Variables
16        this.selection = {start:null, end:null};
17
18        // Objects
19        this.timer = new MoaiEditor.Timer();
20
21        // Intervals
22        this.interval = setInterval(this.onInterval.bind(this), 200);
23    }
24    // ────────────────────────────────────
25    onInterval() {
26
27        // Return if the parent is disabled
28        if (!this.outer.enabled)
29            return;
30
31        // Return if not enough time has elapsed
32        if (!this.timer.due())
33            return;
34
35        // Return if the editor is not enabled and ready
36        if (!moaiEditor.layoutReady)
37            return;
38
39        // Check for cursor/selection change
40        this.checkSelectionChange();
41
42        // Optional debug actions
43        this.debug();
44    }
45    // ────────────────────────────────────
46    onInput() {
47        this.timer.resetPeriod();
48        this.timer.restart();
49        this.checkSelectionChange();
50    }
51    // ────────────────────────────────────
52    checkSelectionChange() {
53        const start = this.outer.textarea.selectionStart;
54        const end = this.outer.textarea.selectionEnd;
55        if (start != this.selection.start  ||  end != this.selection.end) {
56            this.selection = {start:start, end:end};
57            this.timer.resetPeriod();       // Start checking more frequently for changes
58
59            const coords = this.getCoords(start);
60            //console.warn(this.selection);
61            //console.warn(this.getCoords(start));
62
63            const string = this.outer.watcher.string;
64            //console.warn(string[start]+" ");
65        }
66    }
67    // ────────────────────────────────────
68    getCoords(pos) {
69        var i = 0;
70        var row = 0;
71        for (let line of this.outer.watcher.lines) {
72            const end = i + line.length + 1;
73            if (pos < end)
74                return {row:row, col:pos-i};
75            i = end;
76            row += 1;
77        }
78    }
79    // ────────────────────────────────────
80    debug() {
81        // Placeholder
82    }
83}; // End Class
84
85
86