1 import {onChildEvent, onEnterPress, onSelect} from '../services/dom';
2 import {Component} from './component';
4 export class CodeEditor extends Component {
7 * @type {null|SimpleEditorInterface}
15 historyKey = 'code_history';
18 this.container = this.$refs.container;
19 this.popup = this.$el;
20 this.editorInput = this.$refs.editor;
21 this.languageButtons = this.$manyRefs.languageButton;
22 this.languageOptionsContainer = this.$refs.languageOptionsContainer;
23 this.saveButton = this.$refs.saveButton;
24 this.languageInput = this.$refs.languageInput;
25 this.historyDropDown = this.$refs.historyDropDown;
26 this.historyList = this.$refs.historyList;
27 this.favourites = new Set(this.$opts.favourites.split(','));
29 this.setupListeners();
30 this.setupFavourites();
34 this.container.addEventListener('keydown', event => {
35 if (event.ctrlKey && event.key === 'Enter') {
40 onSelect(this.languageButtons, event => {
41 const language = event.target.dataset.lang;
42 this.languageInput.value = language;
43 this.languageInputChange(language);
46 onEnterPress(this.languageInput, () => this.save());
47 this.languageInput.addEventListener('input', () => this.languageInputChange(this.languageInput.value));
48 onSelect(this.saveButton, () => this.save());
50 onChildEvent(this.historyList, 'button', 'click', (event, elem) => {
51 event.preventDefault();
52 const historyTime = elem.dataset.time;
54 this.editor.setContent(this.history[historyTime]);
60 for (const button of this.languageButtons) {
61 this.setupFavouritesForButton(button);
64 this.sortLanguageList();
68 * @param {HTMLButtonElement} button
70 setupFavouritesForButton(button) {
71 const language = button.dataset.lang;
72 let isFavorite = this.favourites.has(language);
73 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
75 onChildEvent(button.parentElement, '.lang-option-favorite-toggle', 'click', () => {
76 isFavorite = !isFavorite;
79 this.favourites.add(language);
81 this.favourites.delete(language);
84 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
86 window.$http.patch('/preferences/update-code-language-favourite', {
91 this.sortLanguageList();
93 button.scrollIntoView({block: 'center', behavior: 'smooth'});
99 const sortedParents = this.languageButtons.sort((a, b) => {
100 const aFav = a.dataset.favourite === 'true';
101 const bFav = b.dataset.favourite === 'true';
105 } if (bFav && !aFav) {
109 return a.dataset.lang > b.dataset.lang ? 1 : -1;
110 }).map(button => button.parentElement);
112 for (const parent of sortedParents) {
113 this.languageOptionsContainer.append(parent);
119 this.callback(this.editor.getContent(), this.languageInput.value);
124 async open(code, language, callback) {
125 this.languageInput.value = language;
126 this.callback = callback;
129 this.languageInputChange(language);
130 this.editor.setContent(code);
134 const Code = await window.importVersioned('code');
136 this.editor = Code.popupEditor(this.editorInput, this.languageInput.value);
140 this.getPopup().show(() => {
148 this.getPopup().hide();
156 return window.$components.firstOnElement(this.popup, 'popup');
159 async updateEditorMode(language) {
160 this.editor.setMode(language, this.editor.getContent());
163 languageInputChange(language) {
164 this.updateEditorMode(language);
165 const inputLang = language.toLowerCase();
167 for (const link of this.languageButtons) {
168 const lang = link.dataset.lang.toLowerCase().trim();
169 const isMatch = inputLang === lang;
170 link.classList.toggle('active', isMatch);
172 link.scrollIntoView({block: 'center', behavior: 'smooth'});
178 this.history = JSON.parse(window.sessionStorage.getItem(this.historyKey) || '{}');
179 const historyKeys = Object.keys(this.history).reverse();
180 this.historyDropDown.classList.toggle('hidden', historyKeys.length === 0);
181 this.historyList.innerHTML = historyKeys.map(key => {
182 const localTime = (new Date(parseInt(key, 10))).toLocaleTimeString();
183 return `<li><button type="button" data-time="${key}" class="text-item">${localTime}</button></li>`;
188 if (!this.editor) return;
189 const code = this.editor.getContent();
192 // Stop if we'd be storing the same as the last item
193 const lastHistoryKey = Object.keys(this.history).pop();
194 if (this.history[lastHistoryKey] === code) return;
196 this.history[String(Date.now())] = code;
197 const historyString = JSON.stringify(this.history);
198 window.sessionStorage.setItem(this.historyKey, historyString);