@bestcodetools/graphql-playground 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ ((app) => {
2
+ const TEMPLATE_VERSION = '20260409c';
3
+
4
+ app.component('responseViewer', {
5
+ templateUrl: `components/response-viewer.html?v=${TEMPLATE_VERSION}`,
6
+ bindings: {
7
+ result: '<',
8
+ api: '=?'
9
+ },
10
+ controller: ['$element', function ($element) {
11
+ const $ctrl = this;
12
+ let editor = null;
13
+ let resizeHandler = null;
14
+
15
+ function ensureResponseMode() {
16
+ if (typeof window.CodeMirror === 'undefined') {
17
+ return;
18
+ }
19
+
20
+ if (window.CodeMirror.modes && window.CodeMirror.modes['graphql-response-json']) {
21
+ return;
22
+ }
23
+
24
+ window.CodeMirror.defineMode('graphql-response-json', function () {
25
+ return {
26
+ startState() {
27
+ return {
28
+ inString: false,
29
+ escaped: false,
30
+ stringTokenType: 'string',
31
+ contextStack: []
32
+ };
33
+ },
34
+ token(stream, state) {
35
+ if (state.inString) {
36
+ let next;
37
+
38
+ while ((next = stream.next()) != null) {
39
+ if (next === '"' && !state.escaped) {
40
+ state.inString = false;
41
+ break;
42
+ }
43
+
44
+ state.escaped = !state.escaped && next === '\\';
45
+ }
46
+
47
+ return state.stringTokenType || 'string';
48
+ }
49
+
50
+ if (stream.eatSpace()) {
51
+ return null;
52
+ }
53
+
54
+ if (stream.peek() === '"') {
55
+ const currentContext = state.contextStack[state.contextStack.length - 1];
56
+ stream.next();
57
+ state.inString = true;
58
+ state.escaped = false;
59
+ state.stringTokenType = currentContext && currentContext.type === 'object' && currentContext.expectKey
60
+ ? 'property'
61
+ : 'string';
62
+ return state.stringTokenType;
63
+ }
64
+
65
+ if (stream.match(/^(?:true|false|null)\b/)) {
66
+ const currentContext = state.contextStack[state.contextStack.length - 1];
67
+ if (currentContext && currentContext.type === 'object' && !currentContext.expectKey) {
68
+ currentContext.expectKey = false;
69
+ }
70
+ return 'atom';
71
+ }
72
+
73
+ if (stream.match(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/)) {
74
+ const currentContext = state.contextStack[state.contextStack.length - 1];
75
+ if (currentContext && currentContext.type === 'object' && !currentContext.expectKey) {
76
+ currentContext.expectKey = false;
77
+ }
78
+ return 'number';
79
+ }
80
+
81
+ if (stream.match(/^[{}\[\],:]/)) {
82
+ const token = stream.current();
83
+ const currentContext = state.contextStack[state.contextStack.length - 1];
84
+
85
+ if (token === '{') {
86
+ state.contextStack.push({ type: 'object', expectKey: true });
87
+ } else if (token === '[') {
88
+ state.contextStack.push({ type: 'array', expectKey: false });
89
+ } else if (token === '}') {
90
+ state.contextStack.pop();
91
+ } else if (token === ']') {
92
+ state.contextStack.pop();
93
+ } else if (token === ':') {
94
+ if (currentContext && currentContext.type === 'object') {
95
+ currentContext.expectKey = false;
96
+ }
97
+ } else if (token === ',') {
98
+ if (currentContext && currentContext.type === 'object') {
99
+ currentContext.expectKey = true;
100
+ }
101
+ }
102
+
103
+ return 'bracket';
104
+ }
105
+
106
+ stream.next();
107
+ return null;
108
+ }
109
+ };
110
+ });
111
+ }
112
+
113
+ function scheduleRefresh() {
114
+ if (!editor) {
115
+ return;
116
+ }
117
+
118
+ window.requestAnimationFrame(() => {
119
+ if (editor) {
120
+ editor.setSize('100%', '100%');
121
+ const wrapper = editor.getWrapperElement();
122
+ const scroller = editor.getScrollerElement();
123
+
124
+ if (wrapper) {
125
+ wrapper.style.width = '100%';
126
+ wrapper.style.height = '100%';
127
+ wrapper.style.flex = '1 1 auto';
128
+ }
129
+
130
+ if (scroller) {
131
+ scroller.style.width = '100%';
132
+ scroller.style.height = '100%';
133
+ }
134
+
135
+ editor.refresh();
136
+ }
137
+ });
138
+ }
139
+
140
+ function createEditor() {
141
+ const textarea = $element[0].querySelector('textarea');
142
+
143
+ if (!textarea || typeof window.CodeMirror === 'undefined') {
144
+ return;
145
+ }
146
+
147
+ ensureResponseMode();
148
+
149
+ editor = window.CodeMirror.fromTextArea(textarea, {
150
+ mode: 'graphql-response-json',
151
+ lineNumbers: true,
152
+ lineWrapping: true,
153
+ readOnly: true,
154
+ viewportMargin: Infinity
155
+ });
156
+
157
+ editor.setValue($ctrl.result || '');
158
+ editor.setSize('100%', '100%');
159
+ scheduleRefresh();
160
+ }
161
+
162
+ $ctrl.$postLink = function () {
163
+ $ctrl.api = $ctrl.api || {};
164
+ $ctrl.api.refresh = scheduleRefresh;
165
+ createEditor();
166
+ resizeHandler = () => scheduleRefresh();
167
+ window.addEventListener('resize', resizeHandler);
168
+ window.setTimeout(scheduleRefresh, 0);
169
+ window.setTimeout(scheduleRefresh, 120);
170
+ };
171
+
172
+ $ctrl.$onChanges = function (changes) {
173
+ if (!editor || !changes.result) {
174
+ return;
175
+ }
176
+
177
+ const nextValue = typeof changes.result.currentValue === 'string' ? changes.result.currentValue : '';
178
+ if (editor.getValue() === nextValue) {
179
+ return;
180
+ }
181
+
182
+ const scrollInfo = editor.getScrollInfo();
183
+ editor.setValue(nextValue);
184
+ editor.scrollTo(scrollInfo.left, scrollInfo.top);
185
+ scheduleRefresh();
186
+ };
187
+
188
+ $ctrl.$onDestroy = function () {
189
+ if (resizeHandler) {
190
+ window.removeEventListener('resize', resizeHandler);
191
+ resizeHandler = null;
192
+ }
193
+
194
+ if (editor) {
195
+ editor.toTextArea();
196
+ editor = null;
197
+ }
198
+ };
199
+ }]
200
+ });
201
+ })(angular.module('app'));