This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
InlineDocsViewer.js
189 lines (155 loc) · 7.3 KB
/
InlineDocsViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/* This code was copied from the QuickDocsRegEx extension and then modified. */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, window, Mustache */
/**
* Inline widget to display WebPlatformDocs JSON data nicely formatted
*/
define(function (require, exports, module) {
'use strict';
// Load Brackets modules
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
InlineWidget = brackets.getModule("editor/InlineWidget").InlineWidget,
KeyEvent = brackets.getModule("utils/KeyEvent"),
NativeApp = brackets.getModule("utils/NativeApp"),
Strings = brackets.getModule("strings");
// Load template
var inlineEditorTemplate = require("text!templates/InlineDocsViewer.html");
// Lines height for scrolling
var SCROLL_LINE_HEIGHT = 40;
// Load CSS
ExtensionUtils.loadStyleSheet(module, "WebPlatformDocs.less");
/**
* @param {!string} keywordName
* @param {!{keyword_name},{keyword_library},{keyword_args},{keyword_doc}} keywordDetails
*/
function InlineDocsViewer(keywordName, keywordDetails) {
InlineWidget.call(this);
var html = Mustache.render(inlineEditorTemplate, keywordDetails);
this.$wrapperDiv = $(html);
this.$htmlContent.append(this.$wrapperDiv);
this._sizeEditorToContent = this._sizeEditorToContent.bind(this);
this._handleWheelScroll = this._handleWheelScroll.bind(this);
this.$scroller = this.$wrapperDiv.find(".scroller");
this.$scroller.on("mousewheel", this._handleWheelScroll);
this._onKeydown = this._onKeydown.bind(this);
}
InlineDocsViewer.prototype = Object.create(InlineWidget.prototype);
InlineDocsViewer.prototype.constructor = InlineDocsViewer;
InlineDocsViewer.prototype.parentClass = InlineWidget.prototype;
InlineDocsViewer.prototype.$wrapperDiv = null;
InlineDocsViewer.prototype.$scroller = null;
/**
* Handle scrolling.
*
* @param {Event} event Keyboard event or mouse scrollwheel event
* @param {boolean} scrollingUp Is event to scroll up?
* @param {DOMElement} scroller Element to scroll
* @return {boolean} indication whether key was handled
*/
InlineDocsViewer.prototype._handleScrolling = function (event, scrollingUp, scroller) {
// We need to block the event from both the host CodeMirror code (by stopping bubbling) and the
// browser's native behavior (by preventing default). We preventDefault() *only* when the docs
// scroller is at its limit (when an ancestor would get scrolled instead); otherwise we'd block
// normal scrolling of the docs themselves.
event.stopPropagation();
if (scrollingUp && scroller.scrollTop === 0) {
event.preventDefault();
return true;
} else if (!scrollingUp && scroller.scrollTop + scroller.clientHeight >= scroller.scrollHeight) {
event.preventDefault();
return true;
}
return false;
};
/** Don't allow scrollwheel/trackpad to bubble up to host editor - makes scrolling docs painful */
InlineDocsViewer.prototype._handleWheelScroll = function (event) {
var scrollingUp = (event.originalEvent.wheelDeltaY > 0),
scroller = event.currentTarget;
// If content has no scrollbar, let host editor scroll normally
if (scroller.clientHeight >= scroller.scrollHeight) {
return;
}
this._handleScrolling(event, scrollingUp, scroller);
};
/**
* Convert keydown events into navigation actions.
*
* @param {KeyboardEvent} event
* @return {boolean} indication whether key was handled
*/
InlineDocsViewer.prototype._onKeydown = function (event) {
var keyCode = event.keyCode,
scroller = this.$scroller[0],
scrollPos;
// Ignore key events with modifier keys
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
return false;
}
// Handle keys that we're interested in
scrollPos = scroller.scrollTop;
switch (keyCode) {
case KeyEvent.DOM_VK_UP:
scrollPos = Math.max(0, scrollPos - SCROLL_LINE_HEIGHT);
break;
case KeyEvent.DOM_VK_PAGE_UP:
scrollPos = Math.max(0, scrollPos - scroller.clientHeight);
break;
case KeyEvent.DOM_VK_DOWN:
scrollPos = Math.min(scroller.scrollHeight - scroller.clientHeight,
scrollPos + SCROLL_LINE_HEIGHT);
break;
case KeyEvent.DOM_VK_PAGE_DOWN:
scrollPos = Math.min(scroller.scrollHeight - scroller.clientHeight,
scrollPos + scroller.clientHeight);
break;
default:
// Ignore other keys
return false;
}
scroller.scrollTop = scrollPos;
// Disallow further processing
event.stopPropagation();
event.preventDefault();
return true;
};
InlineDocsViewer.prototype.onAdded = function () {
InlineDocsViewer.prototype.parentClass.onAdded.apply(this, arguments);
// Set height initially, and again whenever width might have changed (word wrap)
this._sizeEditorToContent();
$(window).on("resize", this._sizeEditorToContent);
// Set focus
this.$scroller[0].focus();
this.$wrapperDiv[0].addEventListener("keydown", this._onKeydown, true);
};
InlineDocsViewer.prototype.onClosed = function () {
InlineDocsViewer.prototype.parentClass.onClosed.apply(this, arguments);
$(window).off("resize", this._sizeEditorToContent);
this.$wrapperDiv[0].removeEventListener("keydown", this._onKeydown, true);
};
InlineDocsViewer.prototype._sizeEditorToContent = function () {
this.hostEditor.setInlineWidgetHeight(this, this.$wrapperDiv.height() + 20, true);
};
module.exports = InlineDocsViewer;
});