2022-01-28 16:00:11 -05:00
|
|
|
import $ from 'jquery';
|
2021-10-16 13:28:04 -04:00
|
|
|
import {initMarkupContent} from '../markup/content.js';
|
2023-04-03 06:06:57 -04:00
|
|
|
import {validateTextareaNonEmpty, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2021-10-21 03:37:43 -04:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2022-01-05 07:17:25 -05:00
|
|
|
async function initRepoWikiFormEditor() {
|
2023-04-03 06:06:57 -04:00
|
|
|
const $editArea = $('.repository.wiki .combo-markdown-editor textarea');
|
2022-01-05 07:17:25 -05:00
|
|
|
if (!$editArea.length) return;
|
|
|
|
|
|
|
|
const $form = $('.repository.wiki.new .ui.form');
|
2023-04-03 06:06:57 -04:00
|
|
|
const $editorContainer = $form.find('.combo-markdown-editor');
|
|
|
|
let editor;
|
2022-01-05 07:17:25 -05:00
|
|
|
|
2023-04-03 06:06:57 -04:00
|
|
|
let renderRequesting = false;
|
|
|
|
let lastContent;
|
|
|
|
const renderEasyMDEPreview = function () {
|
|
|
|
if (renderRequesting) return;
|
2022-01-05 07:17:25 -05:00
|
|
|
|
2023-04-03 06:06:57 -04:00
|
|
|
const $previewFull = $editorContainer.find('.EasyMDEContainer .editor-preview-active');
|
|
|
|
const $previewSide = $editorContainer.find('.EasyMDEContainer .editor-preview-active-side');
|
|
|
|
const $previewTarget = $previewSide.length ? $previewSide : $previewFull;
|
|
|
|
const newContent = $editArea.val();
|
|
|
|
if (editor && $previewTarget.length && lastContent !== newContent) {
|
|
|
|
renderRequesting = true;
|
|
|
|
$.post(editor.previewUrl, {
|
|
|
|
_csrf: csrfToken,
|
|
|
|
mode: editor.previewMode,
|
|
|
|
context: editor.previewContext,
|
|
|
|
text: newContent,
|
|
|
|
wiki: editor.previewWiki,
|
|
|
|
}).done((data) => {
|
|
|
|
lastContent = newContent;
|
|
|
|
$previewTarget.html(`<div class="markup ui segment">${data}</div>`);
|
|
|
|
initMarkupContent();
|
|
|
|
}).always(() => {
|
|
|
|
renderRequesting = false;
|
|
|
|
setTimeout(renderEasyMDEPreview, 1000);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setTimeout(renderEasyMDEPreview, 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
renderEasyMDEPreview();
|
2021-10-27 00:33:22 -04:00
|
|
|
|
2023-04-03 06:06:57 -04:00
|
|
|
editor = await initComboMarkdownEditor($editorContainer, {
|
2023-04-07 13:03:29 -04: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 06:06:57 -04: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', '|',
|
|
|
|
'clean-block', 'preview', 'fullscreen', 'side-by-side', '|', 'gitea-switch-to-textarea'
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2021-10-27 00:33:22 -04:00
|
|
|
|
2022-01-05 07:17:25 -05:00
|
|
|
$form.on('submit', () => {
|
|
|
|
if (!validateTextareaNonEmpty($editArea)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2022-01-05 07:17:25 -05:00
|
|
|
export function initRepoWikiForm() {
|
|
|
|
initRepoWikiFormEditor();
|
2021-10-16 13:28:04 -04:00
|
|
|
}
|