File size: 2,825 Bytes
ef60c31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
require.config({paths: {'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.33.0/min/vs'}});

require(['vs/editor/editor.main'], function() {
    const editor = monaco.editor.create(document.getElementById('editor'), {
        value: '',
        language: 'python',
        theme: 'vs-dark',
        automaticLayout: true
    });
    const myself = Math.random();
    let flag = false; 

    function normalizeContent(content) {
        return content.replace(/\r\n/g, '\n').trim();
    }

    function fetchContent() {
        fetch('/content', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => { 
            if (!response.ok) {
                throw new Error('Whoops, something went wrong');
            } else {
                return response.json()
            }    
        })
        .then(data => {
            const currentContent = editor.getValue();
            let hasChanged = normalizeContent(currentContent) !== normalizeContent(data.content)
            
            if (hasChanged && myself != data.lastEditor) {
                flag = true;
                const selection = editor.getSelection();
                const model = editor.getModel();
                model.pushEditOperations(
                        [],
                        [
                            {
                                range: model.getFullModelRange(),
                                text: data.content,
                            }
                        ]
                    );
                editor.setSelection(selection);
            }
        })
        .catch((error) => {
            console.log(error);
        });
    }

    const fetchDelay = 1000
    function fetchContentLoop() {
        (function loop() {
            setTimeout(() => {
                fetchContent();
                loop();
            }, fetchDelay);
        })();
    }
    fetchContentLoop();

    editor.onDidChangeModelContent(function() {
        if (flag){
            flag = false;
            return;
        } else {
            fetch('/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                        code: editor.getValue(),
                        iam: myself
                })
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Whoops, something went wrong');
                }
            })
            .catch((error) => {
                console.log(error);
            });
        }
    });
});