Fix Shift+Enter sometimes not moving caret
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / redactor2 / plugins / WoltLabKeydown.js
1 $.Redactor.prototype.WoltLabKeydown = function() {
2 "use strict";
3
4 var _tags = [];
5
6 return {
7 init: function () {
8 this.keydown.onArrowDown = (function() {
9 var tags = this.WoltLabKeydown._getBlocks();
10
11 for (var i = 0; i < tags.length; i++) {
12 if (tags[i]) {
13 this.keydown.insertAfterLastElement(tags[i]);
14 return false;
15 }
16 }
17 }).bind(this);
18
19 this.keydown.onArrowUp = (function() {
20 var tags = this.WoltLabKeydown._getBlocks();
21
22 for (var i = 0; i < tags.length; i++) {
23 if (tags[i]) {
24 this.keydown.insertBeforeFirstElement(tags[i]);
25 return false;
26 }
27 }
28 }).bind(this);
29
30 var mpOnEnter = this.keydown.onEnter;
31 this.keydown.onEnter = (function(e) {
32 var isBlockquote = this.keydown.blockquote;
33 if (isBlockquote) this.keydown.blockquote = false;
34
35 mpOnEnter.call(this, e);
36
37 if (isBlockquote) this.keydown.blockquote = isBlockquote;
38 }).bind(this);
39
40 this.keydown.onShiftEnter = (function(e) {
41 this.buffer.set();
42
43 if (this.keydown.pre) {
44 return this.keydown.insertNewLine(e);
45 }
46
47 return this.insert.raw('<br>\u200B');
48 }).bind(this);
49
50 var mpOnTab = this.keydown.onTab;
51 this.keydown.onTab = (function(e, key) {
52 if (!this.keydown.pre && $(this.selection.current()).closest('ul, ol', this.core.editor()[0]).length === 0) {
53 return true;
54 }
55
56 return mpOnTab.call(this, e, key);
57 }).bind(this);
58
59 require(['Core', 'Environment'], (function (Core, Environment) {
60 if (Environment.platform() !== 'desktop') {
61 // ignore mobile devices
62 return;
63 }
64
65 var container = this.$editor[0].closest('form, .message');
66 if (container === null) return;
67
68 var formSubmit = elBySel('.formSubmit', container);
69 if (formSubmit === null) return;
70
71 var submitButton = elBySel('input[type="submit"], button[data-type="save"], button[accesskey="s"]', formSubmit);
72 if (submitButton) {
73 // remove access key
74 submitButton.removeAttribute('accesskey');
75
76 // mimic the same behavior which will also work with more
77 // than one editor instance on the page
78 this.WoltLabEvent.register('keydown', (function (data) {
79 // 83 = [S]
80 if (data.event.which === 83) {
81 var submit = false;
82
83 if (window.navigator.platform.match(/^Mac/)) {
84 if (data.event.ctrlKey && data.event.altKey) {
85 submit = true;
86 }
87 }
88 else if (data.event.altKey && !data.event.ctrlKey) {
89 submit = true;
90 }
91
92 if (submit) {
93 data.cancel = true;
94
95 Core.triggerEvent(submitButton, WCF_CLICK_EVENT);
96 }
97 }
98 }).bind(this));
99 }
100 }).bind(this));
101
102 },
103
104 register: function (tag) {
105 if (_tags.indexOf(tag) === -1) {
106 _tags.push(tag);
107 }
108 },
109
110 _getBlocks: function () {
111 var tags = [this.keydown.blockquote, this.keydown.pre, this.keydown.figcaption];
112
113 for (var i = 0, length = _tags.length; i < length; i++) {
114 tags.push(this.utils.isTag(this.keydown.current, _tags[i]))
115 }
116
117 return tags;
118 }
119 }
120 };