2023-07-09 11:06:13 +00:00
|
|
|
import tinycolor from 'tinycolor2';
|
2020-05-14 16:06:01 +00:00
|
|
|
import {basename, extname, isObject, isDarkTheme} from '../utils.js';
|
2023-05-10 15:50:58 +00:00
|
|
|
import {onInputDebounce} from '../utils/dom.js';
|
2020-05-14 16:06:01 +00:00
|
|
|
|
|
|
|
const languagesByFilename = {};
|
|
|
|
const languagesByExt = {};
|
|
|
|
|
2021-04-08 09:53:00 +00:00
|
|
|
const baseOptions = {
|
|
|
|
fontFamily: 'var(--fonts-monospace)',
|
|
|
|
fontSize: 14, // https://github.com/microsoft/monaco-editor/issues/2242
|
2021-10-19 07:23:58 +00:00
|
|
|
guides: {bracketPairs: false, indentation: false},
|
2021-04-08 09:53:00 +00:00
|
|
|
links: false,
|
|
|
|
minimap: {enabled: false},
|
|
|
|
occurrencesHighlight: false,
|
|
|
|
overviewRulerLanes: 0,
|
|
|
|
renderLineHighlight: 'all',
|
|
|
|
renderLineHighlightOnlyWhenFocus: true,
|
|
|
|
rulers: false,
|
|
|
|
scrollbar: {horizontalScrollbarSize: 6, verticalScrollbarSize: 6},
|
|
|
|
scrollBeyondLastLine: false,
|
2022-10-20 01:54:18 +00:00
|
|
|
automaticLayout: true,
|
2021-04-08 09:53:00 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 16:06:01 +00:00
|
|
|
function getEditorconfig(input) {
|
|
|
|
try {
|
2021-11-22 08:19:01 +00:00
|
|
|
return JSON.parse(input.getAttribute('data-editorconfig'));
|
2020-06-09 21:31:15 +00:00
|
|
|
} catch {
|
2020-05-14 16:06:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initLanguages(monaco) {
|
|
|
|
for (const {filenames, extensions, id} of monaco.languages.getLanguages()) {
|
|
|
|
for (const filename of filenames || []) {
|
|
|
|
languagesByFilename[filename] = id;
|
|
|
|
}
|
|
|
|
for (const extension of extensions || []) {
|
|
|
|
languagesByExt[extension] = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLanguage(filename) {
|
|
|
|
return languagesByFilename[filename] || languagesByExt[extname(filename)] || 'plaintext';
|
|
|
|
}
|
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
function updateEditor(monaco, editor, filename, lineWrapExts) {
|
2021-04-08 09:53:00 +00:00
|
|
|
editor.updateOptions(getFileBasedOptions(filename, lineWrapExts));
|
2020-05-14 16:06:01 +00:00
|
|
|
const model = editor.getModel();
|
2021-11-11 01:52:16 +00:00
|
|
|
const language = model.getLanguageId();
|
2020-11-14 03:57:34 +00:00
|
|
|
const newLanguage = getLanguage(filename);
|
2020-05-14 16:06:01 +00:00
|
|
|
if (language !== newLanguage) monaco.editor.setModelLanguage(model, newLanguage);
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:19:32 +00:00
|
|
|
// export editor for customization - https://github.com/go-gitea/gitea/issues/10409
|
|
|
|
function exportEditor(editor) {
|
|
|
|
if (!window.codeEditors) window.codeEditors = [];
|
|
|
|
if (!window.codeEditors.includes(editor)) window.codeEditors.push(editor);
|
|
|
|
}
|
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
export async function createMonaco(textarea, filename, editorOpts) {
|
2020-05-14 16:06:01 +00:00
|
|
|
const monaco = await import(/* webpackChunkName: "monaco" */'monaco-editor');
|
2020-11-14 03:57:34 +00:00
|
|
|
|
2020-05-14 16:06:01 +00:00
|
|
|
initLanguages(monaco);
|
2020-11-14 03:57:34 +00:00
|
|
|
let {language, ...other} = editorOpts;
|
|
|
|
if (!language) language = getLanguage(filename);
|
2020-05-14 16:06:01 +00:00
|
|
|
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.className = 'monaco-editor-container';
|
2023-05-09 02:35:49 +00:00
|
|
|
textarea.parentNode.append(container);
|
2020-05-14 16:06:01 +00:00
|
|
|
|
2021-04-08 09:53:00 +00:00
|
|
|
// https://github.com/microsoft/monaco-editor/issues/2427
|
2023-07-09 11:06:13 +00:00
|
|
|
// also, monaco can only parse 6-digit hex colors, so we convert the colors to that format
|
2021-04-08 09:53:00 +00:00
|
|
|
const styles = window.getComputedStyle(document.documentElement);
|
2023-07-09 11:06:13 +00:00
|
|
|
const getColor = (name) => tinycolor(styles.getPropertyValue(name).trim()).toString('hex6');
|
2021-04-08 09:53:00 +00:00
|
|
|
|
|
|
|
monaco.editor.defineTheme('gitea', {
|
|
|
|
base: isDarkTheme() ? 'vs-dark' : 'vs',
|
|
|
|
inherit: true,
|
|
|
|
rules: [
|
|
|
|
{
|
2023-07-09 11:06:13 +00:00
|
|
|
background: getColor('--color-code-bg'),
|
2021-04-08 09:53:00 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
colors: {
|
2023-07-09 11:06:13 +00:00
|
|
|
'editor.background': getColor('--color-code-bg'),
|
|
|
|
'editor.foreground': getColor('--color-text'),
|
|
|
|
'editor.inactiveSelectionBackground': getColor('--color-primary-light-4'),
|
|
|
|
'editor.lineHighlightBackground': getColor('--color-editor-line-highlight'),
|
|
|
|
'editor.selectionBackground': getColor('--color-primary-light-3'),
|
|
|
|
'editor.selectionForeground': getColor('--color-primary-light-3'),
|
|
|
|
'editorLineNumber.background': getColor('--color-code-bg'),
|
|
|
|
'editorLineNumber.foreground': getColor('--color-secondary-dark-6'),
|
|
|
|
'editorWidget.background': getColor('--color-body'),
|
|
|
|
'editorWidget.border': getColor('--color-secondary'),
|
|
|
|
'input.background': getColor('--color-input-background'),
|
|
|
|
'input.border': getColor('--color-input-border'),
|
|
|
|
'input.foreground': getColor('--color-input-text'),
|
|
|
|
'scrollbar.shadow': getColor('--color-shadow'),
|
|
|
|
'progressBar.background': getColor('--color-primary'),
|
2021-04-08 09:53:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-09 10:02:19 +00:00
|
|
|
// Quick fix: https://github.com/microsoft/monaco-editor/issues/2962
|
|
|
|
monaco.languages.register({id: 'vs.editor.nullLanguage'});
|
|
|
|
monaco.languages.setLanguageConfiguration('vs.editor.nullLanguage', {});
|
|
|
|
|
2020-05-14 16:06:01 +00:00
|
|
|
const editor = monaco.editor.create(container, {
|
|
|
|
value: textarea.value,
|
2021-04-08 09:53:00 +00:00
|
|
|
theme: 'gitea',
|
2020-11-14 03:57:34 +00:00
|
|
|
language,
|
|
|
|
...other,
|
2020-05-14 16:06:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const model = editor.getModel();
|
|
|
|
model.onDidChangeContent(() => {
|
|
|
|
textarea.value = editor.getValue();
|
|
|
|
textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure
|
|
|
|
});
|
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
exportEditor(editor);
|
2020-05-14 16:06:01 +00:00
|
|
|
|
|
|
|
const loading = document.querySelector('.editor-loading');
|
|
|
|
if (loading) loading.remove();
|
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
return {monaco, editor};
|
|
|
|
}
|
2020-06-03 11:19:32 +00:00
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
function getFileBasedOptions(filename, lineWrapExts) {
|
|
|
|
return {
|
|
|
|
wordWrap: (lineWrapExts || []).includes(extname(filename)) ? 'on' : 'off',
|
|
|
|
};
|
2020-05-14 16:06:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 05:25:41 +00:00
|
|
|
function togglePreviewDisplay(previewable) {
|
|
|
|
const previewTab = document.querySelector('a[data-tab="preview"]');
|
|
|
|
if (!previewTab) return;
|
|
|
|
|
|
|
|
if (previewable) {
|
2023-04-27 02:08:16 +00:00
|
|
|
const newUrl = (previewTab.getAttribute('data-url') || '').replace(/(.*)\/.*/, `$1/markup`);
|
2023-03-26 05:25:41 +00:00
|
|
|
previewTab.setAttribute('data-url', newUrl);
|
|
|
|
previewTab.style.display = '';
|
|
|
|
} else {
|
|
|
|
previewTab.style.display = 'none';
|
|
|
|
// If the "preview" tab was active, user changes the filename to a non-previewable one,
|
|
|
|
// then the "preview" tab becomes inactive (hidden), so the "write" tab should become active
|
|
|
|
if (previewTab.classList.contains('active')) {
|
|
|
|
const writeTab = document.querySelector('a[data-tab="write"]');
|
|
|
|
writeTab.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:12:23 +00:00
|
|
|
export async function createCodeEditor(textarea, filenameInput) {
|
2020-11-14 03:57:34 +00:00
|
|
|
const filename = basename(filenameInput.value);
|
2023-03-26 05:25:41 +00:00
|
|
|
const previewableExts = new Set((textarea.getAttribute('data-previewable-extensions') || '').split(','));
|
2021-11-22 08:19:01 +00:00
|
|
|
const lineWrapExts = (textarea.getAttribute('data-line-wrap-extensions') || '').split(',');
|
2023-03-26 05:25:41 +00:00
|
|
|
const previewable = previewableExts.has(extname(filename));
|
2020-11-14 03:57:34 +00:00
|
|
|
const editorConfig = getEditorconfig(filenameInput);
|
|
|
|
|
2023-03-26 05:25:41 +00:00
|
|
|
togglePreviewDisplay(previewable);
|
2020-05-14 16:06:01 +00:00
|
|
|
|
2020-11-14 03:57:34 +00:00
|
|
|
const {monaco, editor} = await createMonaco(textarea, filename, {
|
2021-04-08 09:53:00 +00:00
|
|
|
...baseOptions,
|
2020-11-14 03:57:34 +00:00
|
|
|
...getFileBasedOptions(filenameInput.value, lineWrapExts),
|
|
|
|
...getEditorConfigOptions(editorConfig),
|
|
|
|
});
|
|
|
|
|
2023-05-10 15:50:58 +00:00
|
|
|
filenameInput.addEventListener('input', onInputDebounce(() => {
|
2020-11-14 03:57:34 +00:00
|
|
|
const filename = filenameInput.value;
|
2023-03-26 05:25:41 +00:00
|
|
|
const previewable = previewableExts.has(extname(filename));
|
|
|
|
togglePreviewDisplay(previewable);
|
2020-11-14 03:57:34 +00:00
|
|
|
updateEditor(monaco, editor, filename, lineWrapExts);
|
2023-03-26 05:25:41 +00:00
|
|
|
}));
|
2020-11-14 03:57:34 +00:00
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEditorConfigOptions(ec) {
|
|
|
|
if (!isObject(ec)) return {};
|
|
|
|
|
|
|
|
const opts = {};
|
|
|
|
opts.detectIndentation = !('indent_style' in ec) || !('indent_size' in ec);
|
|
|
|
if ('indent_size' in ec) opts.indentSize = Number(ec.indent_size);
|
|
|
|
if ('tab_width' in ec) opts.tabSize = Number(ec.tab_width) || opts.indentSize;
|
|
|
|
if ('max_line_length' in ec) opts.rulers = [Number(ec.max_line_length)];
|
|
|
|
opts.trimAutoWhitespace = ec.trim_trailing_whitespace === true;
|
|
|
|
opts.insertSpaces = ec.indent_style === 'space';
|
|
|
|
opts.useTabStops = ec.indent_style === 'tab';
|
2020-05-14 16:06:01 +00:00
|
|
|
return opts;
|
|
|
|
}
|