2022-01-28 21:00:11 +00:00
|
|
|
import $ from 'jquery';
|
2021-10-16 17:28:04 +00:00
|
|
|
import {initMarkupContent} from '../markup/content.js';
|
2022-05-20 02:26:04 +00:00
|
|
|
import {attachEasyMDEToElements, codeMirrorQuickSubmit, importEasyMDE, validateTextareaNonEmpty} from './comp/EasyMDE.js';
|
2021-10-16 17:28:04 +00:00
|
|
|
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
|
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
async function initRepoWikiFormEditor() {
|
2021-10-16 17:28:04 +00:00
|
|
|
const $editArea = $('.repository.wiki textarea#edit_area');
|
2022-01-05 12:17:25 +00:00
|
|
|
if (!$editArea.length) return;
|
|
|
|
|
2021-10-16 17:28:04 +00:00
|
|
|
let sideBySideChanges = 0;
|
|
|
|
let sideBySideTimeout = null;
|
2021-12-10 02:51:27 +00:00
|
|
|
let hasEasyMDE = true;
|
2021-10-27 04:33:22 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
const $form = $('.repository.wiki.new .ui.form');
|
|
|
|
const EasyMDE = await importEasyMDE();
|
|
|
|
const easyMDE = new EasyMDE({
|
|
|
|
autoDownloadFontAwesome: false,
|
|
|
|
element: $editArea[0],
|
|
|
|
forceSync: true,
|
|
|
|
previewRender(plainText, preview) { // Async method
|
|
|
|
// FIXME: still send render request when return back to edit mode
|
|
|
|
const render = function () {
|
|
|
|
sideBySideChanges = 0;
|
|
|
|
if (sideBySideTimeout !== null) {
|
|
|
|
clearTimeout(sideBySideTimeout);
|
|
|
|
sideBySideTimeout = null;
|
|
|
|
}
|
|
|
|
$.post($editArea.data('url'), {
|
|
|
|
_csrf: csrfToken,
|
|
|
|
mode: 'gfm',
|
|
|
|
context: $editArea.data('context'),
|
|
|
|
text: plainText,
|
|
|
|
wiki: true
|
|
|
|
}, (data) => {
|
|
|
|
preview.innerHTML = `<div class="markup ui segment">${data}</div>`;
|
|
|
|
initMarkupContent();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!easyMDE.isSideBySideActive()) {
|
|
|
|
render();
|
|
|
|
} else {
|
|
|
|
// delay preview by keystroke counting
|
|
|
|
sideBySideChanges++;
|
|
|
|
if (sideBySideChanges > 10) {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
// or delay preview by timeout
|
2021-10-16 17:28:04 +00:00
|
|
|
if (sideBySideTimeout !== null) {
|
|
|
|
clearTimeout(sideBySideTimeout);
|
|
|
|
sideBySideTimeout = null;
|
|
|
|
}
|
2022-01-05 12:17:25 +00:00
|
|
|
sideBySideTimeout = setTimeout(render, 600);
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
2022-01-05 12:17:25 +00:00
|
|
|
}, 0);
|
|
|
|
if (!easyMDE.isSideBySideActive()) {
|
|
|
|
return 'Loading...';
|
|
|
|
}
|
|
|
|
return preview.innerHTML;
|
|
|
|
},
|
|
|
|
renderingConfig: {
|
|
|
|
singleLineBreaks: false
|
|
|
|
},
|
|
|
|
indentWithTabs: false,
|
|
|
|
tabSize: 4,
|
|
|
|
spellChecker: false,
|
2022-06-28 17:52:58 +00:00
|
|
|
inputStyle: 'contenteditable', // nativeSpellcheck requires contenteditable
|
|
|
|
nativeSpellcheck: true,
|
2022-01-05 12:17:25 +00:00
|
|
|
toolbar: ['bold', 'italic', 'strikethrough', '|',
|
|
|
|
'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
|
|
|
|
{
|
|
|
|
name: 'code-inline',
|
|
|
|
action(e) {
|
|
|
|
const cm = e.codemirror;
|
|
|
|
const selection = cm.getSelection();
|
|
|
|
cm.replaceSelection(`\`${selection}\``);
|
|
|
|
if (!selection) {
|
|
|
|
const cursorPos = cm.getCursor();
|
|
|
|
cm.setCursor(cursorPos.line, cursorPos.ch - 1);
|
|
|
|
}
|
|
|
|
cm.focus();
|
|
|
|
},
|
|
|
|
className: 'fa fa-angle-right',
|
|
|
|
title: 'Add Inline Code',
|
|
|
|
}, 'code', 'quote', '|', {
|
|
|
|
name: 'checkbox-empty',
|
|
|
|
action(e) {
|
|
|
|
const cm = e.codemirror;
|
|
|
|
cm.replaceSelection(`\n- [ ] ${cm.getSelection()}`);
|
|
|
|
cm.focus();
|
|
|
|
},
|
|
|
|
className: 'fa fa-square-o',
|
|
|
|
title: 'Add Checkbox (empty)',
|
2021-10-16 17:28:04 +00:00
|
|
|
},
|
2022-01-05 12:17:25 +00:00
|
|
|
{
|
|
|
|
name: 'checkbox-checked',
|
|
|
|
action(e) {
|
|
|
|
const cm = e.codemirror;
|
|
|
|
cm.replaceSelection(`\n- [x] ${cm.getSelection()}`);
|
|
|
|
cm.focus();
|
2021-10-16 17:28:04 +00:00
|
|
|
},
|
2022-01-05 12:17:25 +00:00
|
|
|
className: 'fa fa-check-square-o',
|
|
|
|
title: 'Add Checkbox (checked)',
|
|
|
|
}, '|',
|
|
|
|
'unordered-list', 'ordered-list', '|',
|
|
|
|
'link', 'image', 'table', 'horizontal-rule', '|',
|
|
|
|
'clean-block', 'preview', 'fullscreen', 'side-by-side', '|',
|
|
|
|
{
|
|
|
|
name: 'revert-to-textarea',
|
|
|
|
action(e) {
|
|
|
|
e.toTextArea();
|
|
|
|
hasEasyMDE = false;
|
|
|
|
const $root = $form.find('.field.content');
|
|
|
|
const loading = $root.data('loading');
|
|
|
|
$root.append(`<div class="ui bottom tab markup" data-tab="preview">${loading}</div>`);
|
|
|
|
initCompMarkupContentPreviewTab($form);
|
2021-10-16 17:28:04 +00:00
|
|
|
},
|
2022-01-05 12:17:25 +00:00
|
|
|
className: 'fa fa-file',
|
|
|
|
title: 'Revert to simple textarea',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2022-05-20 02:26:04 +00:00
|
|
|
easyMDE.codemirror.setOption('extraKeys', {
|
|
|
|
'Cmd-Enter': codeMirrorQuickSubmit,
|
|
|
|
'Ctrl-Enter': codeMirrorQuickSubmit,
|
|
|
|
});
|
2021-10-27 04:33:22 +00:00
|
|
|
|
2022-05-20 02:26:04 +00:00
|
|
|
attachEasyMDEToElements(easyMDE);
|
2021-10-27 04:33:22 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
$form.on('submit', () => {
|
|
|
|
if (!validateTextareaNonEmpty($editArea)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2022-01-03 16:53:53 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
const $bEdit = $('.repository.wiki.new .previewtabs a[data-tab="write"]');
|
|
|
|
const $bPrev = $('.repository.wiki.new .previewtabs a[data-tab="preview"]');
|
|
|
|
const $toolbar = $('.editor-toolbar');
|
|
|
|
const $bPreview = $('.editor-toolbar button.preview');
|
|
|
|
const $bSideBySide = $('.editor-toolbar a.fa-columns');
|
|
|
|
$bEdit.on('click', (e) => {
|
|
|
|
if (!hasEasyMDE) {
|
2022-01-03 16:53:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-05 12:17:25 +00:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
if ($toolbar.hasClass('disabled-for-preview')) {
|
|
|
|
$bPreview.trigger('click');
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$bPrev.on('click', (e) => {
|
|
|
|
if (!hasEasyMDE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
if (!$toolbar.hasClass('disabled-for-preview')) {
|
|
|
|
$bPreview.trigger('click');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$bPreview.on('click', () => {
|
|
|
|
setTimeout(() => {
|
2021-10-16 17:28:04 +00:00
|
|
|
if ($toolbar.hasClass('disabled-for-preview')) {
|
2022-01-05 12:17:25 +00:00
|
|
|
if ($bEdit.hasClass('active')) {
|
|
|
|
$bEdit.removeClass('active');
|
|
|
|
}
|
|
|
|
if (!$bPrev.hasClass('active')) {
|
|
|
|
$bPrev.addClass('active');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$bEdit.hasClass('active')) {
|
|
|
|
$bEdit.addClass('active');
|
|
|
|
}
|
|
|
|
if ($bPrev.hasClass('active')) {
|
|
|
|
$bPrev.removeClass('active');
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
2022-01-05 12:17:25 +00:00
|
|
|
}, 0);
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$bSideBySide.on('click', () => {
|
|
|
|
sideBySideChanges = 10;
|
|
|
|
});
|
|
|
|
}, 0);
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
export function initRepoWikiForm() {
|
|
|
|
initRepoWikiFormEditor();
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|