editor.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. "use strict";
  2. function do_insert_data(data) {
  3. const name = 'opennamu_edit_textarea';
  4. if(get_select_editor() === 'textarea') {
  5. // https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  6. if(document.selection) {
  7. document.getElementById(name).focus();
  8. let sel = document.selection.createRange();
  9. sel.text = data;
  10. } else if(
  11. document.getElementById(name).selectionStart ||
  12. document.getElementById(name).selectionStart == '0'
  13. ) {
  14. let startPos = document.getElementById(name).selectionStart;
  15. let endPos = document.getElementById(name).selectionEnd;
  16. let myPos = document.getElementById(name).value;
  17. document.getElementById(name).value = myPos.substring(0, startPos) + data + myPos.substring(endPos, myPos.length);
  18. } else {
  19. document.getElementById(name).value += data;
  20. }
  21. } else {
  22. let selection = editor.getSelection();
  23. let id = { major: 1, minor: 1 };
  24. let text = data;
  25. let op = {
  26. identifier: id,
  27. range: selection,
  28. text: text,
  29. forceMoveMarkers: true
  30. };
  31. editor.executeEdits("my-source", [op]);
  32. }
  33. }
  34. // 아직 개편이 더 필요함
  35. function do_paste_image() {
  36. const name = 'opennamu_edit_textarea';
  37. window.addEventListener('DOMContentLoaded', async function() {
  38. let set = await opennamu_get_main_skin_set("main_css_image_paste");
  39. if(set === 'use') {
  40. document.getElementById(name).addEventListener("paste", pasteListener);
  41. }
  42. });
  43. }
  44. function pasteListener(e) {
  45. // find file
  46. if(e.clipboardData && e.clipboardData.items) {
  47. const items = e.clipboardData.items;
  48. const formData = new FormData();
  49. let haveImageInClipboard = false;
  50. let file_name = '';
  51. for(let i = 0; i < items.length; i++) {
  52. if(items[i].type.indexOf("image") !== -1) {
  53. const file = items[i].getAsFile();
  54. const customName = prompt("파일 이름 (확장자 제외)");
  55. if(!customName) {
  56. return alert("파일 이름 없음");
  57. }
  58. file_name = customName + ".png";
  59. const customFile = new File([file], file_name, { type: file.type });
  60. formData.append("f_data[]", customFile);
  61. haveImageInClipboard = true;
  62. e.preventDefault();
  63. break;
  64. }
  65. }
  66. if(!haveImageInClipboard) {
  67. return;
  68. }
  69. // send to server
  70. fetch("/upload", {
  71. method: "POST",
  72. body: formData,
  73. }).then((res) => {
  74. if (res.status === 200 || res.status === 201) {
  75. const url = res.url;
  76. alert(
  77. '업로드 완료 : ' +
  78. '[[파일:' + file_name + ']]'
  79. );
  80. } else {
  81. console.error("[ERROR] PasteUpload Fail :", res.statusText);
  82. if(res.status === 400) {
  83. alert("파일 이름 중복");
  84. } else if(res.status === 401) {
  85. alert("권한 부족");
  86. } else {
  87. alert("업로드 실패");
  88. }
  89. }
  90. }).catch((err) => {
  91. console.error("오류 내역 :", JSON.stringify(err), err);
  92. alert("업로드 실패");
  93. });
  94. }
  95. }
  96. function do_stop_exit() {
  97. window.onbeforeunload = function() {
  98. do_sync_monaco_and_textarea();
  99. let data = document.getElementById('opennamu_edit_textarea').value;
  100. let origin = document.getElementById('opennamu_edit_origin').value;
  101. if(data !== origin) {
  102. return '';
  103. }
  104. }
  105. }
  106. function do_stop_exit_release() {
  107. do_sync_monaco_and_textarea();
  108. window.onbeforeunload = function () {}
  109. }
  110. function opennamu_edit_turn_off_monaco() {
  111. do_sync_monaco_and_textarea();
  112. if(get_select_editor() === 'textarea') {
  113. document.getElementById('opennamu_edit_textarea').style.display = 'none';
  114. document.getElementById('opennamu_monaco_editor').style.display = 'block';
  115. } else {
  116. document.getElementById('opennamu_edit_textarea').style.display = 'block';
  117. document.getElementById('opennamu_monaco_editor').style.display = 'none';
  118. }
  119. }
  120. function do_monaco_to_textarea() {
  121. document.getElementById('opennamu_edit_textarea').value = window.editor.getValue();
  122. }
  123. function do_textarea_to_manaco() {
  124. window.editor.setValue(document.getElementById('opennamu_edit_textarea').value);
  125. }
  126. function get_select_editor() {
  127. if(document.getElementById('opennamu_monaco_editor').style.display === 'none') {
  128. return 'textarea'
  129. } else {
  130. return 'monaco'
  131. }
  132. }
  133. function do_sync_monaco_and_textarea(select = '') {
  134. if(select === 'textarea_to_monaco' || get_select_editor() === 'textarea') {
  135. do_textarea_to_manaco();
  136. } else {
  137. do_monaco_to_textarea();
  138. }
  139. }
  140. // https://github.com/microsoft/monaco-editor/issues/568
  141. class PlaceholderContentWidget {
  142. static ID = 'editor.widget.placeholderHint';
  143. constructor(placeholder, editor) {
  144. this.placeholder = placeholder;
  145. this.editor = editor;
  146. editor.onDidChangeModelContent(() => this.onDidChangeModelContent());
  147. this.onDidChangeModelContent();
  148. }
  149. onDidChangeModelContent() {
  150. if(this.editor.getValue() === '') {
  151. this.editor.addContentWidget(this);
  152. } else {
  153. this.editor.removeContentWidget(this);
  154. }
  155. }
  156. getId() {
  157. return PlaceholderContentWidget.ID;
  158. }
  159. getDomNode() {
  160. if(!this.domNode) {
  161. this.domNode = document.createElement('div');
  162. this.domNode.style.width = 'max-content';
  163. this.domNode.textContent = this.placeholder;
  164. this.domNode.style.fontStyle = 'italic';
  165. this.editor.applyFontInfo(this.domNode);
  166. }
  167. return this.domNode;
  168. }
  169. getPosition() {
  170. return {
  171. position: { lineNumber: 1, column: 1 },
  172. preference: [monaco.editor.ContentWidgetPositionPreference.EXACT],
  173. };
  174. }
  175. dispose() {
  176. this.editor.removeContentWidget(this);
  177. }
  178. }
  179. function do_monaco_init(monaco_thema, markup = "") {
  180. require.config({ paths: { 'vs' : 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.40.0/min/vs' }});
  181. require.config({ 'vs/nls' : { availableLanguages: { '*' : 'ko' } }});
  182. require(["vs/editor/editor.main"], function () {
  183. monaco.languages.register({ id : "namumark" });
  184. monaco.languages.setMonarchTokensProvider("namumark", {
  185. tokenizer : {
  186. root : [
  187. [/\[/, "namumark-color"],
  188. [/\]/, "namumark-color"],
  189. [/\{/, "namumark-color"],
  190. [/\}/, "namumark-color"],
  191. [/'/, "namumark-color"],
  192. [/-/, "namumark-color"],
  193. [/~/, "namumark-color"],
  194. [/=/, "namumark-color"],
  195. [/_/, "namumark-color"],
  196. [/\^/, "namumark-color"],
  197. [/,/, "namumark-color"],
  198. [/\\/, "namumark-color"],
  199. [/\*/, "namumark-color"],
  200. ],
  201. },
  202. });
  203. let thema_set = [["namumark", "vs"], ["namumark-vs-dark", "vs-dark"]]
  204. for(let for_a = 0; for_a < thema_set.length; for_a++) {
  205. monaco.editor.defineTheme(thema_set[for_a][0], {
  206. base : thema_set[for_a][1],
  207. inherit : true,
  208. rules : [
  209. { token : "namumark-color", foreground : "d94844" },
  210. ],
  211. colors : {},
  212. });
  213. }
  214. window.editor = monaco.editor.create(document.getElementById('opennamu_monaco_editor'), {
  215. value : document.getElementById('opennamu_edit_textarea').value,
  216. language : 'namumark',
  217. automaticLayout : true,
  218. wordWrap : true,
  219. theme : "namumark" + (monaco_thema === "" ? "" : "-" + monaco_thema)
  220. });
  221. new PlaceholderContentWidget(document.getElementById('opennamu_edit_textarea').placeholder, window.editor);
  222. });
  223. }
  224. function opennamu_do_editor_preview() {
  225. do_sync_monaco_and_textarea();
  226. const input = document.querySelector('#opennamu_edit_textarea');
  227. if(input !== null) {
  228. let name = "test";
  229. if(document.getElementById('opennamu_editor_doc_name')) {
  230. name = document.getElementById('opennamu_editor_doc_name').value.replace(/&amp;/g, '&');
  231. }
  232. opennamu_do_render('opennamu_preview_area', input.value, name);
  233. }
  234. }
  235. function opennamu_do_editor_temp_save() {
  236. do_sync_monaco_and_textarea();
  237. const input = document.querySelector('#opennamu_edit_textarea');
  238. if(input !== null) {
  239. localStorage.setItem("key", input.value);
  240. }
  241. }
  242. function opennamu_do_editor_temp_save_load() {
  243. const data = localStorage.getItem("key");
  244. if(data !== null) {
  245. const input = document.querySelector('#opennamu_edit_textarea');
  246. if(input !== null) {
  247. input.value = data;
  248. }
  249. do_textarea_to_manaco();
  250. }
  251. }