2020-03-13 15:44:24 +05:30
|
|
|
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
|
2019-12-04 20:38:33 +05:30
|
|
|
import { s__, sprintf } from '~/locale';
|
2020-07-28 23:09:34 +05:30
|
|
|
import Tracking from '~/tracking';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
const MARKDOWN_LINK_TEXT = {
|
|
|
|
markdown: '[Link Title](page-slug)',
|
|
|
|
rdoc: '{Link title}[link:page-slug]',
|
|
|
|
asciidoc: 'link:page-slug[Link title]',
|
|
|
|
org: '[[page-slug]]',
|
|
|
|
};
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
const TRACKING_EVENT_NAME = 'view_wiki_page';
|
|
|
|
const TRACKING_CONTEXT_SCHEMA = 'iglu:com.gitlab/wiki_page_context/jsonschema/1-0-0';
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
export default class Wikis {
|
|
|
|
constructor() {
|
|
|
|
this.sidebarEl = document.querySelector('.js-wiki-sidebar');
|
|
|
|
this.sidebarExpanded = false;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
const sidebarToggles = document.querySelectorAll('.js-sidebar-wiki-toggle');
|
|
|
|
for (let i = 0; i < sidebarToggles.length; i += 1) {
|
|
|
|
sidebarToggles[i].addEventListener('click', e => this.handleToggleSidebar(e));
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
this.isNewWikiPage = Boolean(document.querySelector('.js-new-wiki-page'));
|
|
|
|
this.editTitleInput = document.querySelector('form.wiki-form #wiki_title');
|
|
|
|
this.commitMessageInput = document.querySelector('form.wiki-form #wiki_message');
|
|
|
|
this.commitMessageI18n = this.isNewWikiPage
|
|
|
|
? s__('WikiPageCreate|Create %{pageTitle}')
|
|
|
|
: s__('WikiPageEdit|Update %{pageTitle}');
|
|
|
|
|
|
|
|
if (this.editTitleInput) {
|
|
|
|
// Initialize the commit message on load
|
|
|
|
if (this.editTitleInput.value) this.setWikiCommitMessage(this.editTitleInput.value);
|
|
|
|
|
|
|
|
// Set the commit message as the page title is changed
|
|
|
|
this.editTitleInput.addEventListener('keyup', e => this.handleWikiTitleChange(e));
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
window.addEventListener('resize', () => this.renderSidebar());
|
|
|
|
this.renderSidebar();
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
const changeFormatSelect = document.querySelector('#wiki_format');
|
|
|
|
const linkExample = document.querySelector('.js-markup-link-example');
|
|
|
|
|
|
|
|
if (changeFormatSelect) {
|
|
|
|
changeFormatSelect.addEventListener('change', e => {
|
|
|
|
linkExample.innerHTML = MARKDOWN_LINK_TEXT[e.target.value];
|
|
|
|
});
|
|
|
|
}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
const wikiTextarea = document.querySelector('form.wiki-form #wiki_content');
|
|
|
|
const wikiForm = document.querySelector('form.wiki-form');
|
|
|
|
|
|
|
|
if (wikiTextarea) {
|
|
|
|
wikiTextarea.addEventListener('input', () => {
|
|
|
|
window.onbeforeunload = () => '';
|
|
|
|
});
|
|
|
|
|
|
|
|
wikiForm.addEventListener('submit', () => {
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
});
|
|
|
|
}
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
Wikis.trackPageView();
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
handleWikiTitleChange(e) {
|
|
|
|
this.setWikiCommitMessage(e.target.value);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
setWikiCommitMessage(rawTitle) {
|
|
|
|
let title = rawTitle;
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
// Replace hyphens with spaces
|
|
|
|
if (title) title = title.replace(/-+/g, ' ');
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const newCommitMessage = sprintf(this.commitMessageI18n, { pageTitle: title }, false);
|
2019-12-04 20:38:33 +05:30
|
|
|
this.commitMessageInput.value = newCommitMessage;
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
handleToggleSidebar(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.sidebarExpanded = !this.sidebarExpanded;
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
static sidebarCanCollapse() {
|
|
|
|
const bootstrapBreakpoint = bp.getBreakpointSize();
|
2020-03-13 15:44:24 +05:30
|
|
|
return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
renderSidebar() {
|
|
|
|
if (!this.sidebarEl) return;
|
|
|
|
const { classList } = this.sidebarEl;
|
|
|
|
if (this.sidebarExpanded || !Wikis.sidebarCanCollapse()) {
|
|
|
|
if (!classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.remove('right-sidebar-collapsed');
|
|
|
|
classList.add('right-sidebar-expanded');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
} else if (classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.add('right-sidebar-collapsed');
|
|
|
|
classList.remove('right-sidebar-expanded');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
static trackPageView() {
|
|
|
|
const wikiPageContent = document.querySelector('.js-wiki-page-content[data-tracking-context]');
|
|
|
|
if (!wikiPageContent) return;
|
|
|
|
|
|
|
|
Tracking.event(document.body.dataset.page, TRACKING_EVENT_NAME, {
|
|
|
|
label: TRACKING_EVENT_NAME,
|
|
|
|
context: {
|
|
|
|
schema: TRACKING_CONTEXT_SCHEMA,
|
|
|
|
data: JSON.parse(wikiPageContent.dataset.trackingContext),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|