2021-10-16 10:28:04 -07:00
|
|
|
import {initMarkupContent} from '../markup/content.js';
|
2023-04-03 03:06:57 -07:00
|
|
|
import {validateTextareaNonEmpty, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
|
2023-06-23 12:51:43 -07:00
|
|
|
import {fomanticMobileScreen} from '../modules/fomantic.js';
|
2024-02-20 03:37:37 -07:00
|
|
|
import {POST} from '../modules/fetch.js';
|
2021-10-16 10:28:04 -07:00
|
|
|
|
2022-01-05 05:17:25 -07:00
|
|
|
async function initRepoWikiFormEditor() {
|
2024-02-20 03:37:37 -07:00
|
|
|
const editArea = document.querySelector('.repository.wiki .combo-markdown-editor textarea');
|
|
|
|
if (!editArea) return;
|
2022-01-05 05:17:25 -07:00
|
|
|
|
2024-02-20 03:37:37 -07:00
|
|
|
const form = document.querySelector('.repository.wiki.new .ui.form');
|
|
|
|
const editorContainer = form.querySelector('.combo-markdown-editor');
|
2023-04-03 03:06:57 -07:00
|
|
|
let editor;
|
2022-01-05 05:17:25 -07:00
|
|
|
|
2023-04-03 03:06:57 -07:00
|
|
|
let renderRequesting = false;
|
|
|
|
let lastContent;
|
2024-02-20 03:37:37 -07:00
|
|
|
const renderEasyMDEPreview = async function () {
|
2023-04-03 03:06:57 -07:00
|
|
|
if (renderRequesting) return;
|
2022-01-05 05:17:25 -07:00
|
|
|
|
2024-02-20 03:37:37 -07:00
|
|
|
const previewFull = editorContainer.querySelector('.EasyMDEContainer .editor-preview-active');
|
|
|
|
const previewSide = editorContainer.querySelector('.EasyMDEContainer .editor-preview-active-side');
|
|
|
|
const previewTarget = previewSide || previewFull;
|
|
|
|
const newContent = editArea.value;
|
|
|
|
if (editor && previewTarget && lastContent !== newContent) {
|
2023-04-03 03:06:57 -07:00
|
|
|
renderRequesting = true;
|
2024-02-20 03:37:37 -07:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('mode', editor.previewMode);
|
|
|
|
formData.append('context', editor.previewContext);
|
|
|
|
formData.append('text', newContent);
|
|
|
|
formData.append('wiki', editor.previewWiki);
|
|
|
|
try {
|
|
|
|
const response = await POST(editor.previewUrl, {data: formData});
|
|
|
|
const data = await response.text();
|
2023-04-03 03:06:57 -07:00
|
|
|
lastContent = newContent;
|
2024-02-20 03:37:37 -07:00
|
|
|
previewTarget.innerHTML = `<div class="markup ui segment">${data}</div>`;
|
2023-04-03 03:06:57 -07:00
|
|
|
initMarkupContent();
|
2024-02-20 03:37:37 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error rendering preview:', error);
|
|
|
|
} finally {
|
2023-04-03 03:06:57 -07:00
|
|
|
renderRequesting = false;
|
|
|
|
setTimeout(renderEasyMDEPreview, 1000);
|
2024-02-20 03:37:37 -07:00
|
|
|
}
|
2023-04-03 03:06:57 -07:00
|
|
|
} else {
|
|
|
|
setTimeout(renderEasyMDEPreview, 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
renderEasyMDEPreview();
|
2021-10-26 21:33:22 -07:00
|
|
|
|
2024-02-20 03:37:37 -07:00
|
|
|
editor = await initComboMarkdownEditor(editorContainer, {
|
2023-04-07 10:03:29 -07:00
|
|
|
useScene: 'wiki',
|
|
|
|
// EasyMDE has some problems of height definition, it has inline style height 300px by default, so we also use inline styles to override it.
|
|
|
|
// And another benefit is that we only need to write the style once for both editors.
|
|
|
|
// TODO: Move height style to CSS after EasyMDE removal.
|
|
|
|
editorHeights: {minHeight: '300px', height: 'calc(100vh - 600px)'},
|
2023-04-03 03:06:57 -07:00
|
|
|
previewMode: 'gfm',
|
|
|
|
previewWiki: true,
|
|
|
|
easyMDEOptions: {
|
|
|
|
previewRender: (_content, previewTarget) => previewTarget.innerHTML, // disable builtin preview render
|
|
|
|
toolbar: ['bold', 'italic', 'strikethrough', '|',
|
|
|
|
'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
|
|
|
|
'gitea-code-inline', 'code', 'quote', '|', 'gitea-checkbox-empty', 'gitea-checkbox-checked', '|',
|
|
|
|
'unordered-list', 'ordered-list', '|',
|
|
|
|
'link', 'image', 'table', 'horizontal-rule', '|',
|
2024-03-22 07:06:53 -07:00
|
|
|
'preview', 'fullscreen', 'side-by-side', '|', 'gitea-switch-to-textarea',
|
2023-04-03 03:06:57 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2021-10-26 21:33:22 -07:00
|
|
|
|
2024-02-20 03:37:37 -07:00
|
|
|
form.addEventListener('submit', (e) => {
|
|
|
|
if (!validateTextareaNonEmpty(editArea)) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-01-05 05:17:25 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-16 10:28:04 -07:00
|
|
|
|
2023-06-23 12:51:43 -07:00
|
|
|
function collapseWikiTocForMobile(collapse) {
|
|
|
|
if (collapse) {
|
|
|
|
document.querySelector('.wiki-content-toc details')?.removeAttribute('open');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:17:25 -07:00
|
|
|
export function initRepoWikiForm() {
|
2023-06-23 12:51:43 -07:00
|
|
|
if (!document.querySelector('.page-content.repository.wiki')) return;
|
|
|
|
|
|
|
|
fomanticMobileScreen.addEventListener('change', (e) => collapseWikiTocForMobile(e.matches));
|
|
|
|
collapseWikiTocForMobile(fomanticMobileScreen.matches);
|
|
|
|
|
2022-01-05 05:17:25 -07:00
|
|
|
initRepoWikiFormEditor();
|
2021-10-16 10:28:04 -07:00
|
|
|
}
|