1 import {onChildEvent, onEnterPress, onSelect} from '../services/dom';
2 import {Component} from './component';
4 export class CodeEditor extends Component {
7 * @type {null|SimpleEditorInterface}
19 cancelCallback = null;
23 historyKey = 'code_history';
26 this.container = this.$refs.container;
27 this.popup = this.$el;
28 this.editorInput = this.$refs.editor;
29 this.languageButtons = this.$manyRefs.languageButton;
30 this.languageOptionsContainer = this.$refs.languageOptionsContainer;
31 this.saveButton = this.$refs.saveButton;
32 this.languageInput = this.$refs.languageInput;
33 this.historyDropDown = this.$refs.historyDropDown;
34 this.historyList = this.$refs.historyList;
35 this.favourites = new Set(this.$opts.favourites.split(','));
37 this.setupListeners();
38 this.setupFavourites();
42 this.container.addEventListener('keydown', event => {
43 if (event.ctrlKey && event.key === 'Enter') {
48 onSelect(this.languageButtons, event => {
49 const language = event.target.dataset.lang;
50 this.languageInput.value = language;
51 this.languageInputChange(language);
54 onEnterPress(this.languageInput, () => this.save());
55 this.languageInput.addEventListener('input', () => this.languageInputChange(this.languageInput.value));
56 onSelect(this.saveButton, () => this.save());
58 onChildEvent(this.historyList, 'button', 'click', (event, elem) => {
59 event.preventDefault();
60 const historyTime = elem.dataset.time;
62 this.editor.setContent(this.history[historyTime]);
68 for (const button of this.languageButtons) {
69 this.setupFavouritesForButton(button);
72 this.sortLanguageList();
76 * @param {HTMLButtonElement} button
78 setupFavouritesForButton(button) {
79 const language = button.dataset.lang;
80 let isFavorite = this.favourites.has(language);
81 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
83 onChildEvent(button.parentElement, '.lang-option-favorite-toggle', 'click', () => {
84 isFavorite = !isFavorite;
87 this.favourites.add(language);
89 this.favourites.delete(language);
92 button.setAttribute('data-favourite', isFavorite ? 'true' : 'false');
94 window.$http.patch('/preferences/update-code-language-favourite', {
99 this.sortLanguageList();
101 button.scrollIntoView({block: 'center', behavior: 'smooth'});
107 const sortedParents = this.languageButtons.sort((a, b) => {
108 const aFav = a.dataset.favourite === 'true';
109 const bFav = b.dataset.favourite === 'true';
113 } if (bFav && !aFav) {
117 return a.dataset.lang > b.dataset.lang ? 1 : -1;
118 }).map(button => button.parentElement);
120 for (const parent of sortedParents) {
121 this.languageOptionsContainer.append(parent);
126 if (this.saveCallback) {
127 this.saveCallback(this.editor.getContent(), this.languageInput.value);
132 async open(code, language, direction, saveCallback, cancelCallback) {
133 this.languageInput.value = language;
134 this.saveCallback = saveCallback;
135 this.cancelCallback = cancelCallback;
138 this.languageInputChange(language);
139 this.editor.setContent(code);
140 this.setDirection(direction);
144 const Code = await window.importVersioned('code');
146 this.editor = Code.popupEditor(this.editorInput, this.languageInput.value);
150 this.getPopup().show(() => {
154 if (this.cancelCallback) {
155 this.cancelCallback();
160 setDirection(direction) {
161 const target = this.editorInput.parentElement;
163 target.setAttribute('dir', direction);
165 target.removeAttribute('dir');
170 this.getPopup().hide();
178 return window.$components.firstOnElement(this.popup, 'popup');
181 async updateEditorMode(language) {
182 this.editor.setMode(language, this.editor.getContent());
185 languageInputChange(language) {
186 this.updateEditorMode(language);
187 const inputLang = language.toLowerCase();
189 for (const link of this.languageButtons) {
190 const lang = link.dataset.lang.toLowerCase().trim();
191 const isMatch = inputLang === lang;
192 link.classList.toggle('active', isMatch);
194 link.scrollIntoView({block: 'center', behavior: 'smooth'});
200 this.history = JSON.parse(window.sessionStorage.getItem(this.historyKey) || '{}');
201 const historyKeys = Object.keys(this.history).reverse();
202 this.historyDropDown.classList.toggle('hidden', historyKeys.length === 0);
203 this.historyList.innerHTML = historyKeys.map(key => {
204 const localTime = (new Date(parseInt(key, 10))).toLocaleTimeString();
205 return `<li><button type="button" data-time="${key}" class="text-item">${localTime}</button></li>`;
210 if (!this.editor) return;
211 const code = this.editor.getContent();
214 // Stop if we'd be storing the same as the last item
215 const lastHistoryKey = Object.keys(this.history).pop();
216 if (this.history[lastHistoryKey] === code) return;
218 this.history[String(Date.now())] = code;
219 const historyString = JSON.stringify(this.history);
220 window.sessionStorage.setItem(this.historyKey, historyString);