Project

General

Profile

Feature #30459 » 0001-Add-keyboard-shortcut-to-toggle-between-Edit-Preview.patch

Marius BĂLTEANU, 2021-01-02 22:26

View differences:

public/javascripts/jstoolbar/jstoolbar.js
21 21
*/
22 22

  
23 23
/* Modified by JP LANG for textile formatting */
24
let lastJstPreviewed = null;
24 25

  
25 26
function jsToolBar(textarea) {
26 27
  if (!document.createElement) { return; }
......
53 54

  
54 55
  var This = this;
55 56

  
57
  this.textarea.onkeydown = function(event) { This.keyboardShortcuts.call(This, event); };
58

  
56 59
  this.editTab = new jsTab('Edit', true);
57 60
  this.editTab.onclick = function(event) { This.hidePreview.call(This, event); return false; };
58 61

  
......
401 404
  },
402 405
  showPreview: function(event) {
403 406
    if (event.target.classList.contains('selected')) { return; }
407
    lastJstPreviewed = this.toolbarBlock;
404 408
    this.preview.setAttribute('style', 'min-height: ' + this.textarea.clientHeight + 'px;')
405 409
    this.toolbar.classList.add('hidden');
406 410
    this.textarea.classList.add('hidden');
407 411
    this.preview.classList.remove('hidden');
408 412
    this.tabsBlock.getElementsByClassName('tab-edit')[0].classList.remove('selected');
409 413
    event.target.classList.add('selected');
410

  
411 414
  },
412 415
  hidePreview: function(event) {
413 416
    if (event.target.classList.contains('selected')) { return; }
414 417
    this.toolbar.classList.remove('hidden');
415 418
    this.textarea.classList.remove('hidden');
419
    this.textarea.focus();
416 420
    this.preview.classList.add('hidden');
417 421
    this.tabsBlock.getElementsByClassName('tab-preview')[0].classList.remove('selected');
418 422
    event.target.classList.add('selected');
419 423
  },
424
  keyboardShortcuts: function(e) {
425
    if (isToogleEditPreviewShortcut(e)) {
426
      // Switch to preview only if tab edit is selected when the event triggered.
427
      if (this.tabsBlock.querySelector('.tab-edit.selected')) {
428
        e.stopPropagation();
429
        e.preventDefault();
430
        this.tabsBlock.getElementsByClassName('tab-preview')[0].click();
431
      }
432
    }
433
  },
420 434
  stripBaseURL: function(url) {
421 435
    if (this.base_url != '') {
422 436
      var pos = url.indexOf(this.base_url);
......
507 521
  });
508 522
  return false;
509 523
};
524

  
525
$(document).keydown(function(e) {
526
  if (isToogleEditPreviewShortcut(e)) {
527
    if (lastJstPreviewed !== null) {
528
      e.preventDefault();
529
      e.stopPropagation();
530
      lastJstPreviewed.querySelector('.tab-edit').click();
531
      lastJstPreviewed = null;
532
    }
533
  }
534
});
535

  
536
function isToogleEditPreviewShortcut(e) {
537
  if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.key.toLowerCase() === 'p') {
538
    return true;
539
  } else {
540
    return false;
541
  }
542
}
(4-4/5)