1 import {onChildEvent, onEnterPress, onSelect} from "../services/dom";
2 import {Component} from "./component";
5 export class CodeEditor extends Component {
8 * @type {null|SimpleEditorInterface}
14 historyKey = 'code_history';
17 this.container = this.$refs.container;
18 this.popup = this.$el;
19 this.editorInput = this.$refs.editor;
20 this.languageButtons = this.$manyRefs.languageButton;
21 this.languageOptionsContainer = this.$refs.languageOptionsContainer;
22 this.saveButton = this.$refs.saveButton;
23 this.languageInput = this.$refs.languageInput;
24 this.historyDropDown = this.$refs.historyDropDown;
25 this.historyList = this.$refs.historyList;
26 this.favourites = new Set(this.$opts.favourites.split(','));
28 this.setupListeners();
29 this.setupFavourites();
33 this.container.addEventListener('keydown', event => {
34 if (event.ctrlKey && event.key === 'Enter') {
39 onSelect(this.languageButtons, event => {
40 const language = event.target.dataset.lang;
41 this.languageInput.value = language;
42 this.languageInputChange(language);
45 onEnterPress(this.languageInput, e => this.save());
46 this.languageInput.addEventListener('input', e => this.languageInputChange(this.languageInput.value));
47 onSelect(this.saveButton, e => this.save());
49 onChildEvent(this.historyList, 'button', 'click', (event, elem) => {
50 event.preventDefault();
51 const historyTime = elem.dataset.time;
53 this.editor.setContent(this.history[historyTime]);
59 for (const button of this.languageButtons) {
60 this.setupFavouritesForButton(button);
63 this.sortLanguageList();
67 * @param {HTMLButtonElement} button
69 setupFavouritesForButton(button) {
70 const language = button.dataset.lang;
71 let isFavorite = this.favourites.has(language);
72 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
74 onChildEvent(button.parentElement, '.lang-option-favorite-toggle', 'click', () => {
75 isFavorite = !isFavorite;
76 isFavorite ? this.favourites.add(language) : this.favourites.delete(language);
77 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
79 window.$http.patch('/preferences/update-code-language-favourite', {
84 this.sortLanguageList();
86 button.scrollIntoView({block: "center", behavior: "smooth"});
92 const sortedParents = this.languageButtons.sort((a, b) => {
93 const aFav = a.dataset.favourite === 'true';
94 const bFav = b.dataset.favourite === 'true';
98 } else if (bFav && !aFav) {
102 return a.dataset.lang > b.dataset.lang ? 1 : -1;
103 }).map(button => button.parentElement);
105 for (const parent of sortedParents) {
106 this.languageOptionsContainer.append(parent);
112 this.callback(this.editor.getContent(), this.languageInput.value);
117 async open(code, language, callback) {
118 this.languageInput.value = language;
119 this.callback = callback;
122 this.languageInputChange(language);
123 this.editor.setContent(code);
127 const Code = await window.importVersioned('code');
129 this.editor = Code.popupEditor(this.editorInput, this.languageInput.value);
133 this.getPopup().show(() => {
141 this.getPopup().hide();
149 return window.$components.firstOnElement(this.popup, 'popup');
152 async updateEditorMode(language) {
153 this.editor.setMode(language, this.editor.getContent());
156 languageInputChange(language) {
157 this.updateEditorMode(language);
158 const inputLang = language.toLowerCase();
160 for (const link of this.languageButtons) {
161 const lang = link.dataset.lang.toLowerCase().trim();
162 const isMatch = inputLang === lang;
163 link.classList.toggle('active', isMatch);
165 link.scrollIntoView({block: "center", behavior: "smooth"});
171 this.history = JSON.parse(window.sessionStorage.getItem(this.historyKey) || '{}');
172 const historyKeys = Object.keys(this.history).reverse();
173 this.historyDropDown.classList.toggle('hidden', historyKeys.length === 0);
174 this.historyList.innerHTML = historyKeys.map(key => {
175 const localTime = (new Date(parseInt(key))).toLocaleTimeString();
176 return `<li><button type="button" data-time="${key}" class="text-item">${localTime}</button></li>`;
181 if (!this.editor) return;
182 const code = this.editor.getContent();
185 // Stop if we'd be storing the same as the last item
186 const lastHistoryKey = Object.keys(this.history).pop();
187 if (this.history[lastHistoryKey] === code) return;
189 this.history[String(Date.now())] = code;
190 const historyString = JSON.stringify(this.history);
191 window.sessionStorage.setItem(this.historyKey, historyString);