6ec4ffc4ce8622568f9468ac957107a347cce6d5
[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 var mpOnTab = this.keydown.onTab;
41 this.keydown.onTab = (function(e, key) {
42 if (!this.keydown.pre && $(this.selection.current()).closest('ul, ol', this.core.editor()[0]).length === 0) {
43 return true;
44 }
45
46 return mpOnTab.call(this, e, key);
47 }).bind(this);
48
49 require(['Core', 'Environment'], (function (Core, Environment) {
50 if (Environment.platform() !== 'desktop') {
51 // ignore mobile devices
52 return;
53 }
54
55 var container = this.$editor[0].closest('form, .message');
56 if (container === null) return;
57
58 var formSubmit = elBySel('.formSubmit', container);
59 if (formSubmit === null) return;
60
61 var submitButton = elBySel('input[type="submit"], button[data-type="save"], button[accesskey="s"]', formSubmit);
62 if (submitButton) {
63 // remove access key
64 submitButton.removeAttribute('accesskey');
65
66 // mimic the same behavior which will also work with more
67 // than one editor instance on the page
68 this.WoltLabEvent.register('keydown', (function (data) {
69 // 83 = [S]
70 if (data.event.which === 83) {
71 var submit = false;
72
73 if (window.navigator.platform.match(/^Mac/)) {
74 if (data.event.ctrlKey && data.event.altKey) {
75 submit = true;
76 }
77 }
78 else if (data.event.altKey && !data.event.ctrlKey) {
79 submit = true;
80 }
81
82 if (submit) {
83 data.cancel = true;
84
85 Core.triggerEvent(submitButton, WCF_CLICK_EVENT);
86 }
87 }
88 }).bind(this));
89 }
90 }).bind(this));
91
92 },
93
94 register: function (tag) {
95 if (_tags.indexOf(tag) === -1) {
96 _tags.push(tag);
97 }
98 },
99
100 _getBlocks: function () {
101 var tags = [this.keydown.blockquote, this.keydown.pre, this.keydown.figcaption];
102
103 for (var i = 0, length = _tags.length; i < length; i++) {
104 tags.push(this.utils.isTag(this.keydown.current, _tags[i]))
105 }
106
107 return tags;
108 }
109 }
110 };