crowi.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* eslint-disable react/jsx-filename-extension */
  2. import { pathUtils } from 'growi-commons';
  3. const entities = require('entities');
  4. const escapeStringRegexp = require('escape-string-regexp');
  5. require('jquery.cookie');
  6. require('bootstrap-select');
  7. require('./thirdparty-js/agile-admin');
  8. const Crowi = {};
  9. if (!window) {
  10. window = {};
  11. }
  12. window.Crowi = Crowi;
  13. /**
  14. * set 'data-caret-line' attribute that will be processed when 'shown.bs.tab' event fired
  15. * @param {number} line
  16. */
  17. Crowi.setCaretLineData = function(line) {
  18. const pageEditorDom = document.querySelector('#page-editor');
  19. pageEditorDom.setAttribute('data-caret-line', line);
  20. };
  21. /**
  22. * invoked when;
  23. *
  24. * 1. 'shown.bs.tab' event fired
  25. */
  26. Crowi.setCaretLineAndFocusToEditor = function() {
  27. // get 'data-caret-line' attributes
  28. const pageEditorDom = document.querySelector('#page-editor');
  29. if (pageEditorDom == null) {
  30. return;
  31. }
  32. const { appContainer } = window;
  33. const editorContainer = appContainer.getContainer('EditorContainer');
  34. const line = pageEditorDom.getAttribute('data-caret-line') || 0;
  35. editorContainer.setCaretLine(+line);
  36. // reset data-caret-line attribute
  37. pageEditorDom.removeAttribute('data-caret-line');
  38. // focus
  39. editorContainer.focusToEditor();
  40. };
  41. // original: middleware.swigFilter
  42. Crowi.userPicture = function(user) {
  43. if (!user) {
  44. return '/images/icons/user.svg';
  45. }
  46. return user.image || '/images/icons/user.svg';
  47. };
  48. Crowi.modifyScrollTop = function() {
  49. const offset = 10;
  50. const hash = window.location.hash;
  51. if (hash === '') {
  52. return;
  53. }
  54. const pageHeader = document.querySelector('#page-header');
  55. if (!pageHeader) {
  56. return;
  57. }
  58. const pageHeaderRect = pageHeader.getBoundingClientRect();
  59. const sectionHeader = Crowi.findSectionHeader(hash);
  60. if (sectionHeader === null) {
  61. return;
  62. }
  63. let timeout = 0;
  64. if (window.scrollY === 0) {
  65. timeout = 200;
  66. }
  67. setTimeout(() => {
  68. const sectionHeaderRect = sectionHeader.getBoundingClientRect();
  69. if (sectionHeaderRect.top >= pageHeaderRect.bottom) {
  70. return;
  71. }
  72. window.scrollTo(0, (window.scrollY - pageHeaderRect.height - offset));
  73. }, timeout);
  74. };
  75. Crowi.handleKeyEHandler = (event) => {
  76. // ignore when dom that has 'modal in' classes exists
  77. if (document.getElementsByClassName('modal in').length > 0) {
  78. return;
  79. }
  80. // show editor
  81. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  82. event.preventDefault();
  83. };
  84. Crowi.handleKeyCHandler = (event) => {
  85. // ignore when dom that has 'modal in' classes exists
  86. if (document.getElementsByClassName('modal in').length > 0) {
  87. return;
  88. }
  89. // show modal to create a page
  90. $('#create-page').modal();
  91. event.preventDefault();
  92. };
  93. Crowi.handleKeyCtrlSlashHandler = (event) => {
  94. // show modal to create a page
  95. $('#shortcuts-modal').modal('toggle');
  96. event.preventDefault();
  97. };
  98. Crowi.initAffix = () => {
  99. const $affixContent = $('#page-header');
  100. if ($affixContent.length > 0) {
  101. const $affixContentContainer = $('.row.bg-title');
  102. const containerHeight = $affixContentContainer.outerHeight(true);
  103. $affixContent.affix({
  104. offset: {
  105. top() {
  106. return $('.navbar').outerHeight(true) + containerHeight;
  107. },
  108. },
  109. });
  110. $('[data-affix-disable]').on('click', function(e) {
  111. const $elm = $($(this).data('affix-disable'));
  112. $(window).off('.affix');
  113. $elm.removeData('affix').removeClass('affix affix-top affix-bottom');
  114. return false;
  115. });
  116. $affixContentContainer.css({ 'min-height': containerHeight });
  117. }
  118. };
  119. Crowi.initClassesByOS = function() {
  120. // add classes to cmd-key by OS
  121. const platform = navigator.platform.toLowerCase();
  122. const isMac = (platform.indexOf('mac') > -1);
  123. document.querySelectorAll('.system-version .cmd-key').forEach((element) => {
  124. if (isMac) {
  125. element.classList.add('mac');
  126. }
  127. else {
  128. element.classList.add('win');
  129. }
  130. });
  131. document.querySelectorAll('#shortcuts-modal .cmd-key').forEach((element) => {
  132. if (isMac) {
  133. element.classList.add('mac');
  134. }
  135. else {
  136. element.classList.add('win', 'key-longer');
  137. }
  138. });
  139. };
  140. Crowi.findHashFromUrl = function(url) {
  141. let match;
  142. /* eslint-disable no-cond-assign */
  143. if (match = url.match(/#(.+)$/)) {
  144. return `#${match[1]}`;
  145. }
  146. /* eslint-enable no-cond-assign */
  147. return '';
  148. };
  149. Crowi.findSectionHeader = function(hash) {
  150. if (hash.length === 0) {
  151. return;
  152. }
  153. // omit '#'
  154. const id = hash.replace('#', '');
  155. // don't use jQuery and document.querySelector
  156. // because hash may containe Base64 encoded strings
  157. const elem = document.getElementById(id);
  158. if (elem != null && elem.tagName.match(/h\d+/i)) { // match h1, h2, h3...
  159. return elem;
  160. }
  161. return null;
  162. };
  163. Crowi.unhighlightSelectedSection = function(hash) {
  164. const elem = Crowi.findSectionHeader(hash);
  165. if (elem != null) {
  166. elem.classList.remove('highlighted');
  167. }
  168. };
  169. Crowi.highlightSelectedSection = function(hash) {
  170. const elem = Crowi.findSectionHeader(hash);
  171. if (elem != null) {
  172. elem.classList.add('highlighted');
  173. }
  174. };
  175. $(() => {
  176. const appContainer = window.appContainer;
  177. const websocketContainer = appContainer.getContainer('WebsocketContainer');
  178. const config = appContainer.getConfig();
  179. const pageId = $('#content-main').data('page-id');
  180. // const revisionId = $('#content-main').data('page-revision-id');
  181. // const revisionCreatedAt = $('#content-main').data('page-revision-created');
  182. // const currentUser = $('#content-main').data('current-user');
  183. const isSeen = $('#content-main').data('page-is-seen');
  184. const pagePath = $('#content-main').data('path');
  185. const isSavedStatesOfTabChanges = config.isSavedStatesOfTabChanges;
  186. $('[data-toggle="popover"]').popover();
  187. $('[data-toggle="tooltip"]').tooltip();
  188. $('[data-tooltip-stay]').tooltip('show');
  189. $('#toggle-sidebar').click((e) => {
  190. const $mainContainer = $('.main-container');
  191. if ($mainContainer.hasClass('aside-hidden')) {
  192. $('.main-container').removeClass('aside-hidden');
  193. $.cookie('aside-hidden', 0, { expires: 30, path: '/' });
  194. }
  195. else {
  196. $mainContainer.addClass('aside-hidden');
  197. $.cookie('aside-hidden', 1, { expires: 30, path: '/' });
  198. }
  199. return false;
  200. });
  201. if ($.cookie('aside-hidden') === 1) {
  202. $('.main-container').addClass('aside-hidden');
  203. }
  204. $('.copy-link').on('click', function() {
  205. $(this).select();
  206. });
  207. $('#create-page').on('shown.bs.modal', (e) => {
  208. // quick hack: replace from server side rendering "date" to client side "date"
  209. const today = new Date();
  210. const month = (`0${today.getMonth() + 1}`).slice(-2);
  211. const day = (`0${today.getDate()}`).slice(-2);
  212. const dateString = `${today.getFullYear()}/${month}/${day}`;
  213. $('#create-page-today .page-today-suffix').text(`/${dateString}/`);
  214. $('#create-page-today .page-today-input2').data('prefix', `/${dateString}/`);
  215. // focus
  216. $('#create-page-today .page-today-input2').eq(0).focus();
  217. });
  218. $('#create-page-today').submit(function(e) {
  219. let prefix1 = $('input.page-today-input1', this).data('prefix');
  220. let prefix2 = $('input.page-today-input2', this).data('prefix');
  221. const input1 = $('input.page-today-input1', this).val();
  222. const input2 = $('input.page-today-input2', this).val();
  223. if (input1 === '') {
  224. prefix1 = 'メモ';
  225. }
  226. if (input2 === '') {
  227. prefix2 = prefix2.slice(0, -1);
  228. }
  229. window.location.href = `${prefix1 + input1 + prefix2 + input2}#edit`;
  230. return false;
  231. });
  232. $('#create-page-under-tree').submit(function(e) {
  233. let name = $('input', this).val();
  234. if (!name.match(/^\//)) {
  235. name = `/${name}`;
  236. }
  237. if (name.match(/.+\/$/)) {
  238. name = name.substr(0, name.length - 1);
  239. }
  240. window.location.href = `${pathUtils.encodePagePath(name)}#edit`;
  241. return false;
  242. });
  243. // rename/unportalize
  244. $('#renamePage, #unportalize').on('shown.bs.modal', (e) => {
  245. $('#renamePage #newPageName').focus();
  246. $('#renamePage .msg, #unportalize .msg').hide();
  247. });
  248. $('#renamePageForm, #unportalize-form').submit(function(e) {
  249. // create name-value map
  250. const nameValueMap = {};
  251. $(this).serializeArray().forEach((obj) => {
  252. nameValueMap[obj.name] = obj.value; // nameValueMap.new_path is renamed page path
  253. });
  254. nameValueMap.socketClientId = websocketContainer.getSocketClientId();
  255. $.ajax({
  256. type: 'POST',
  257. url: '/_api/pages.rename',
  258. data: nameValueMap,
  259. dataType: 'json',
  260. })
  261. .done((res) => {
  262. // error
  263. if (!res.ok) {
  264. const linkPath = pathUtils.normalizePath(nameValueMap.new_path);
  265. $('#renamePage .msg, #unportalize .msg').hide();
  266. $(`#renamePage .msg-${res.code}, #unportalize .msg-${res.code}`).show();
  267. $('#renamePage #linkToNewPage, #unportalize #linkToNewPage').html(`
  268. <a href="${linkPath}">${linkPath} <i class="icon-login"></i></a>
  269. `);
  270. }
  271. else {
  272. const page = res.page;
  273. window.location.href = `${page.path}?renamed=${pagePath}`;
  274. }
  275. });
  276. return false;
  277. });
  278. // duplicate
  279. $('#duplicatePage').on('shown.bs.modal', (e) => {
  280. $('#duplicatePage #duplicatePageName').focus();
  281. $('#duplicatePage .msg').hide();
  282. });
  283. $('#duplicatePageForm, #unportalize-form').submit(function(e) {
  284. // create name-value map
  285. const nameValueMap = {};
  286. $(this).serializeArray().forEach((obj) => {
  287. nameValueMap[obj.name] = obj.value; // nameValueMap.new_path is duplicated page path
  288. });
  289. nameValueMap.socketClientId = websocketContainer.getSocketClientId();
  290. $.ajax({
  291. type: 'POST',
  292. url: '/_api/pages.duplicate',
  293. data: nameValueMap,
  294. dataType: 'json',
  295. }).done((res) => {
  296. // error
  297. if (!res.ok) {
  298. const linkPath = pathUtils.normalizePath(nameValueMap.new_path);
  299. $('#duplicatePage .msg').hide();
  300. $(`#duplicatePage .msg-${res.code}`).show();
  301. $('#duplicatePage #linkToNewPage').html(`
  302. <a href="${linkPath}">${linkPath} <i class="icon-login"></i></a>
  303. `);
  304. }
  305. else {
  306. const page = res.page;
  307. window.location.href = `${page.path}?duplicated=${pagePath}`;
  308. }
  309. });
  310. return false;
  311. });
  312. // delete
  313. $('#deletePage').on('shown.bs.modal', (e) => {
  314. $('#deletePage .msg').hide();
  315. });
  316. $('#delete-page-form').submit((e) => {
  317. // create name-value map
  318. const nameValueMap = {};
  319. $('#delete-page-form').serializeArray().forEach((obj) => {
  320. nameValueMap[obj.name] = obj.value;
  321. });
  322. nameValueMap.socketClientId = websocketContainer.getSocketClientId();
  323. $.ajax({
  324. type: 'POST',
  325. url: '/_api/pages.remove',
  326. data: nameValueMap,
  327. dataType: 'json',
  328. }).done((res) => {
  329. // error
  330. if (!res.ok) {
  331. $('#deletePage .msg').hide();
  332. $(`#deletePage .msg-${res.code}`).show();
  333. }
  334. else {
  335. const page = res.page;
  336. window.location.href = page.path;
  337. }
  338. });
  339. return false;
  340. });
  341. // Put Back
  342. $('#putBackPage').on('shown.bs.modal', (e) => {
  343. $('#putBackPage .msg').hide();
  344. });
  345. $('#revert-delete-page-form').submit((e) => {
  346. $.ajax({
  347. type: 'POST',
  348. url: '/_api/pages.revertRemove',
  349. data: $('#revert-delete-page-form').serialize(),
  350. dataType: 'json',
  351. }).done((res) => {
  352. // error
  353. if (!res.ok) {
  354. $('#putBackPage .msg').hide();
  355. $(`#putBackPage .msg-${res.code}`).show();
  356. }
  357. else {
  358. const page = res.page;
  359. window.location.href = page.path;
  360. }
  361. });
  362. return false;
  363. });
  364. $('#unlink-page-form').submit((e) => {
  365. $.ajax({
  366. type: 'POST',
  367. url: '/_api/pages.unlink',
  368. data: $('#unlink-page-form').serialize(),
  369. dataType: 'json',
  370. })
  371. .done((res) => {
  372. if (!res.ok) {
  373. $('#delete-errors').html(`<i class="fa fa-times-circle"></i> ${res.error}`);
  374. $('#delete-errors').addClass('alert-danger');
  375. }
  376. else {
  377. window.location.href = `${res.path}?unlinked=true`;
  378. }
  379. });
  380. return false;
  381. });
  382. $('#create-portal-button').on('click', (e) => {
  383. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  384. $('body').addClass('on-edit');
  385. $('body').addClass('builtin-editor');
  386. const path = $('.content-main').data('path');
  387. if (path !== '/' && $('.content-main').data('page-id') === '') {
  388. const upperPage = path.substr(0, path.length - 1);
  389. $.get('/_api/pages.get', { path: upperPage }, (res) => {
  390. if (res.ok && res.page) {
  391. $('#portal-warning-modal').modal('show');
  392. }
  393. });
  394. }
  395. });
  396. $('#portal-form-close').on('click', (e) => {
  397. $('#edit').removeClass('active');
  398. $('body').removeClass('on-edit');
  399. $('body').removeClass('builtin-editor');
  400. window.location.hash = '#';
  401. });
  402. /*
  403. * wrap short path with <strong></strong>
  404. */
  405. $('#view-list .page-list-ul-flat .page-list-link').each(function() {
  406. const $link = $(this);
  407. /* eslint-disable-next-line no-unused-vars */
  408. const text = $link.text();
  409. let path = decodeURIComponent($link.data('path'));
  410. const shortPath = decodeURIComponent($link.data('short-path')); // convert to string
  411. if (path == null || shortPath == null) {
  412. // continue
  413. return;
  414. }
  415. path = entities.encodeHTML(path);
  416. const pattern = `${escapeStringRegexp(entities.encodeHTML(shortPath))}(/)?$`;
  417. $link.html(path.replace(new RegExp(pattern), `<strong>${shortPath}$1</strong>`));
  418. });
  419. if (pageId) {
  420. // for Crowi Template LangProcessor
  421. $('.template-create-button', $('#revision-body')).on('click', function() {
  422. const path = $(this).data('path');
  423. const templateId = $(this).data('template');
  424. const template = $(`#${templateId}`).html();
  425. const editorContainer = appContainer.getContainer('EditorContainer');
  426. editorContainer.saveDraft(path, template);
  427. window.location.href = `${path}#edit`;
  428. });
  429. if (!isSeen) {
  430. $.post('/_api/pages.seen', { page_id: pageId }, (res) => {
  431. // ignore unless response has error
  432. if (res.ok && res.seenUser) {
  433. $('#content-main').data('page-is-seen', 1);
  434. }
  435. });
  436. }
  437. // presentation
  438. let presentaionInitialized = false;
  439. const $b = $('body');
  440. $(document).on('click', '.toggle-presentation', function(e) {
  441. const $a = $(this);
  442. e.preventDefault();
  443. $b.toggleClass('overlay-on');
  444. if (!presentaionInitialized) {
  445. presentaionInitialized = true;
  446. $('<iframe />').attr({
  447. src: $a.attr('href'),
  448. }).appendTo($('#presentation-container'));
  449. }
  450. }).on('click', '.fullscreen-layer', () => {
  451. $b.toggleClass('overlay-on');
  452. });
  453. } // end if pageId
  454. // tab changing handling
  455. $('a[href="#revision-body"]').on('show.bs.tab', () => {
  456. appContainer.setState({ editorMode: null });
  457. });
  458. $('a[href="#edit"]').on('show.bs.tab', () => {
  459. appContainer.setState({ editorMode: 'builtin' });
  460. $('body').addClass('on-edit');
  461. $('body').addClass('builtin-editor');
  462. });
  463. $('a[href="#edit"]').on('hide.bs.tab', () => {
  464. $('body').removeClass('on-edit');
  465. $('body').removeClass('builtin-editor');
  466. });
  467. $('a[href="#hackmd"]').on('show.bs.tab', () => {
  468. appContainer.setState({ editorMode: 'hackmd' });
  469. $('body').addClass('on-edit');
  470. $('body').addClass('hackmd');
  471. });
  472. $('a[href="#hackmd"]').on('hide.bs.tab', () => {
  473. $('body').removeClass('on-edit');
  474. $('body').removeClass('hackmd');
  475. });
  476. // hash handling
  477. if (isSavedStatesOfTabChanges) {
  478. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', () => {
  479. window.location.hash = '#revision-history';
  480. window.history.replaceState('', 'History', '#revision-history');
  481. });
  482. $('a[data-toggle="tab"][href="#edit"]').on('show.bs.tab', () => {
  483. window.location.hash = '#edit';
  484. window.history.replaceState('', 'Edit', '#edit');
  485. });
  486. $('a[data-toggle="tab"][href="#hackmd"]').on('show.bs.tab', () => {
  487. window.location.hash = '#hackmd';
  488. window.history.replaceState('', 'HackMD', '#hackmd');
  489. });
  490. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', () => {
  491. // couln't solve https://github.com/weseek/crowi-plus/issues/119 completely -- 2017.07.03 Yuki Takei
  492. window.location.hash = '#';
  493. window.history.replaceState('', '', window.location.href);
  494. });
  495. }
  496. else {
  497. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', () => {
  498. window.history.replaceState('', 'History', '#revision-history');
  499. });
  500. $('a[data-toggle="tab"][href="#edit"]').on('show.bs.tab', () => {
  501. window.history.replaceState('', 'Edit', '#edit');
  502. });
  503. $('a[data-toggle="tab"][href="#hackmd"]').on('show.bs.tab', () => {
  504. window.history.replaceState('', 'HackMD', '#hackmd');
  505. });
  506. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', () => {
  507. window.history.replaceState('', '', window.location.href.replace(window.location.hash, ''));
  508. });
  509. // replace all href="#edit" link behaviors
  510. $(document).on('click', 'a[href="#edit"]', () => {
  511. window.location.replace('#edit');
  512. });
  513. }
  514. // focus to editor when 'shown.bs.tab' event fired
  515. $('a[href="#edit"]').on('shown.bs.tab', (e) => {
  516. Crowi.setCaretLineAndFocusToEditor();
  517. });
  518. });
  519. window.addEventListener('load', (e) => {
  520. const { appContainer } = window;
  521. // hash on page
  522. if (window.location.hash) {
  523. if ((window.location.hash === '#edit' || window.location.hash === '#edit-form') && $('.tab-pane#edit').length > 0) {
  524. appContainer.setState({ editorMode: 'builtin' });
  525. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  526. $('body').addClass('on-edit');
  527. $('body').addClass('builtin-editor');
  528. // focus
  529. Crowi.setCaretLineAndFocusToEditor();
  530. }
  531. else if (window.location.hash === '#hackmd' && $('.tab-pane#hackmd').length > 0) {
  532. appContainer.setState({ editorMode: 'hackmd' });
  533. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  534. $('body').addClass('on-edit');
  535. $('body').addClass('hackmd');
  536. }
  537. else if (window.location.hash === '#revision-history' && $('.tab-pane#revision-history').length > 0) {
  538. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  539. }
  540. }
  541. const crowi = window.crowi;
  542. if (crowi && crowi.users && crowi.users.length !== 0) {
  543. const totalUsers = crowi.users.length;
  544. const $listLiker = $('.page-list-liker');
  545. $listLiker.each((i, liker) => {
  546. const count = $(liker).data('count') || 0;
  547. if (count / totalUsers > 0.05) {
  548. $(liker).addClass('popular-page-high');
  549. // 5%
  550. }
  551. else if (count / totalUsers > 0.02) {
  552. $(liker).addClass('popular-page-mid');
  553. // 2%
  554. }
  555. else if (count / totalUsers > 0.005) {
  556. $(liker).addClass('popular-page-low');
  557. // 0.5%
  558. }
  559. });
  560. const $listSeer = $('.page-list-seer');
  561. $listSeer.each((i, seer) => {
  562. const count = $(seer).data('count') || 0;
  563. if (count / totalUsers > 0.10) {
  564. // 10%
  565. $(seer).addClass('popular-page-high');
  566. }
  567. else if (count / totalUsers > 0.05) {
  568. // 5%
  569. $(seer).addClass('popular-page-mid');
  570. }
  571. else if (count / totalUsers > 0.02) {
  572. // 2%
  573. $(seer).addClass('popular-page-low');
  574. }
  575. });
  576. }
  577. Crowi.highlightSelectedSection(window.location.hash);
  578. Crowi.modifyScrollTop();
  579. Crowi.initAffix();
  580. Crowi.initClassesByOS();
  581. });
  582. window.addEventListener('hashchange', (e) => {
  583. Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
  584. Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
  585. Crowi.modifyScrollTop();
  586. // hash on page
  587. if (window.location.hash) {
  588. if (window.location.hash === '#edit') {
  589. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  590. }
  591. else if (window.location.hash === '#hackmd') {
  592. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  593. }
  594. else if (window.location.hash === '#revision-history') {
  595. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  596. }
  597. }
  598. else {
  599. $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
  600. }
  601. });
  602. window.addEventListener('keydown', (event) => {
  603. const target = event.target;
  604. // ignore when target dom is input
  605. const inputPattern = /^input|textinput|textarea$/i;
  606. if (inputPattern.test(target.tagName) || target.isContentEditable) {
  607. return;
  608. }
  609. switch (event.key) {
  610. case 'e':
  611. if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
  612. Crowi.handleKeyEHandler(event);
  613. }
  614. break;
  615. case 'c':
  616. if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
  617. Crowi.handleKeyCHandler(event);
  618. }
  619. break;
  620. case '/':
  621. if (event.ctrlKey || event.metaKey) {
  622. Crowi.handleKeyCtrlSlashHandler(event);
  623. }
  624. break;
  625. default:
  626. }
  627. });
  628. // adjust min-height of page for print temporarily
  629. window.onbeforeprint = function() {
  630. $('#page-wrapper').css('min-height', '0px');
  631. };