aboutsummaryrefslogtreecommitdiff
path: root/public/edit.html
blob: 6938c022c314fb24d8db1493854140bd45d0e00c (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
171
172
173
174
175
176
<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport"
        content="width=device-width,minimum-scale=1,initial-scale=1">
  <title>Plugins examples</title>

  <script src="/js/qcm.js"></script>
  <script src="/js/hmd.min.js"></script>
  <link rel="stylesheet" type="text/css" href="/css/style.css">
  <link rel="stylesheet" type="text/css" href="/css/contenu.css">

  <link rel="stylesheet" type="text/css" href="/css/katex.min.css">
<style>
html {
  height: 100vh;
  width: 100vv;
  overflow-y: hidden;
}
body {
  display: grid;
  grid-template-columns: 50% 50%;
  grid-template-row: 100vv;
  grid-template-areas: "edit preview";
  height: 100vh;
  width:  100vv;
  margin: 0;
  padding: 0;
}
#edit {
  grid-area: edit;
  padding: 20px;
  background-color: #EEE;
  resize: none;
}
#preview {
  margin-left: 5px;
  grid-area: preview;
  overflow-y: scroll;
  overflow-x: hidden;
  min-height: 100vh;
}
</style>
</head>
<body>
  <textarea id="edit" contenteditable="true" spellcheck="false" placeholder="Write here..." autofocus=true></textarea>
  <div id="preview" class="view" contenteditable="false">If you can read this line more thant <em>15s</em> there is a bug in the matrix.</div>

  <!-- This is the bundle generated by rollup.js -->
<script>
let edit = document.getElementById('edit');
let preview = document.getElementById('preview');
let modification = true;

const saveButton = document.createElement('button');
saveButton.classList.add('raw_button');
let tmpDiv = document.createElement('div');
let tmpDiv2 = document.createElement('div');

let password = "";

tmpDiv2.appendChild(document.createTextNode('Save'))
tmpDiv.appendChild(tmpDiv2);
saveButton.appendChild(tmpDiv);
saveButton.style = "margin-right: 10px;";


edit.value = hmd.defaultValue;


async function saveMD() {
  // Default options are marked with *
  const url = window.location.href;
  console.log(url);

  const data = {};
  data.text = edit.value;
  data.password = password;

  const response = await fetch(url, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
  }).then( res => {
    if (res.status >= 400 && res.status < 600) {
      throw new Error(`${res.status}Bad error code`);
    }
    return res;
  }).then( res => {
  }).catch( err => {
    console.log('Catch !');
    if (!password) {
      password = window.prompt("You shall NOT pass !");
      window.alert('Try again !');
    } else {
      window.alert(`${err} - Erreur fichier \
${window.location.pathname.replace(/\/edit/g, '/md')}`);
    }
  });
}

function nodeScriptReplace(node) {
  if (node.tagName === 'SCRIPT') {
    node.parentNode.replaceChild(nodeScriptClone(node), node);
  } else if (node.children) {
    for (const child of node.children) {
      nodeScriptReplace(child);
    }
  }

  return node;
}
function nodeScriptClone(node) {
  const script = document.createElement('script');

  script.text = node.innerHTML;
  [...node.attributes].forEach(attr => {
    script.setAttribute(attr.name, attr.value);
  });
  return script;
}



let interval = setInterval( () => {
  if( modification ) {
    hmd(edit.value, (err, file) => {
      let finalString = String(file);
      if( finalString )  {
        preview.innerHTML = String(file);
        preview.appendChild(saveButton);
        nodeScriptReplace(preview);
        modification = false;
      }
    });
  }
}, 500);

edit.addEventListener('input', () => {
  modification = true;
}, false);

saveButton.addEventListener('click', saveMD, false);

document.addEventListener("DOMContentLoaded", function(event) {
  let url = window.location.href;
  url = url.replace(/\/edit/g, '/md');
  url += '?raw=true';

  fetch(url).
    then(function(response) {
      if(response.ok) {
        return response.text();
      } else {
        console.error(`${response.status} - Erreur fichier \
${window.location.pathname.replace(/\/edit/g, '/md')}`);
      }
    })
    .then(function(data) {
      if( data ) {
        edit.value = data;
      }
    })
    .catch(function(error) {
      console.log('Il y a eu un problème avec l\'opération fetch: ' + error.message);
    });
});




  </script>
</body>
</html>