Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | 108x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 65x 65x 65x 11x 11x 11x 11x 2x 2x 2x 2x 2x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 11x 54x 65x 4x 65x 65x 65x 65x 22x 22x 22x 22x 22x 22x 22x 4x 4x 4x 4x 4x 108x 108x 12x 12x 2x 12x 10x 340x 340x 340x 340x 335x 335x 335x 335x 108x 108x 17x 17x 17x 17x 17x 1x 17x 17x 17x 19x 17x 17x 9x 9x 22x 9x 6x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 2x 1x 1x 3x 1x 1x 1x 1x 17x 8x 8x 17x 43x 2x 2x 43x 43x 108x 108x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 108x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 108x 108x 108x 108x 108x 108x 4x 108x 108x 108x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 108x 108x 108x 108x 12x 12x 44x 44x 44x 18x 18x 18x 18x 18x 18x 18x 18x 18x 12x 13x 6x 44x 26x 44x 44x 44x 32x 32x 44x 6x 12x 12x 108x 108x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 108x 108x 108x 108x 108x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 105x 105x 105x 105x 105x 105x 108x 105x 108x 108x 108x 110x 110x 108x 108x 108x 108x 108x 108x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 109x 108x 108x 108x 108x 108x 108x 135x 108x 108x 108x 111x 111x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 109x 109x 109x 108x 108x 108x 108x 108x | import { html, TemplateResult } from 'lit';
import ExcellentParser, { Expression } from './ExcellentParser';
import {
CompletionOption,
CompletionProperty,
CompletionResult,
CompletionSchema,
CompletionType
} from '../interfaces';
import { getStore, Store } from '../store/Store';
import { renderMarkdownInline } from '../markdown';
export const messageParser = new ExcellentParser('@', [
'contact',
'fields',
'globals',
'urns'
]);
export const sessionParser = new ExcellentParser('@', [
'contact',
'fields',
'globals',
'locals',
'urns',
'results',
'input',
'run',
'child',
'parent',
'node',
'webhook',
'ticket',
'trigger',
'resume'
]);
export const renderCompletionOption = (
option: CompletionOption,
selected: boolean
): TemplateResult => {
if (option.signature) {
const argStart = option.signature.indexOf('(');
const name = option.signature.substr(0, argStart);
const args = option.signature.substr(argStart);
return html`
<div style="${selected ? 'font-weight: 400' : ''}">
<div style="display:inline-block;margin-right: 5px">ƒ</div>
<div style="display:inline-block">${name}</div>
${selected
? html`
<div
style="display:inline-block; font-weight: 300; font-size: 85%"
>
${args}
</div>
<div class="detail">${renderMarkdownInline(option.summary)}</div>
`
: null}
</div>
`;
}
return html`
<div>
<div style="${selected ? 'font-weight: 400' : ''}">${option.name}</div>
${selected
? html` <div style="font-size: 85%">${option.summary}</div> `
: null}
</div>
`;
};
export const getFunctions = (
functions: CompletionOption[],
query: string
): CompletionOption[] => {
if (!query) {
return functions;
}
return functions.filter((option: CompletionOption) => {
if (option.signature) {
return option.signature.indexOf((query || '').toLowerCase()) === 0;
}
return false;
});
};
/**
* Takes a dot query and returns the completions options at the current level
* @param dotQuery query such as "contact.first_n"
*/
export const getCompletions = (
schema: CompletionSchema,
dotQuery: string,
session: boolean,
store: Store
): CompletionOption[] => {
const parts = (dotQuery || '').split('.');
let currentProps: CompletionProperty[] = session
? schema.root
: schema.root_no_session;
if (!currentProps) {
return [];
}
let prefix = '';
let part = '';
while (parts.length > 0) {
part = parts.shift();
if (part) {
// eslint-disable-next-line
const nextProp = currentProps.find(
(prop: CompletionProperty) => prop.key === part
);
if (nextProp) {
// eslint-disable-next-line
const nextType = schema.types.find(
(type: CompletionType) => type.name === nextProp.type
);
if (nextType && nextType.properties) {
currentProps = nextType.properties;
prefix += part + '.';
} else if (nextType && nextType.property_template) {
prefix += part + '.';
const template = nextType.property_template;
const keys = store.getCompletions(nextType.name);
if (keys) {
currentProps = keys.map((key: string) => ({
key: template.key.replace('{key}', key),
help: template.help.replace('{key}', key),
type: template.type
}));
} else {
currentProps = [];
}
} else {
// eslint-disable-next-line
currentProps = currentProps.filter((prop: CompletionProperty) =>
prop.key.startsWith(part.toLowerCase())
);
break;
}
} else {
// eslint-disable-next-line
currentProps = currentProps.filter((prop: CompletionProperty) =>
prop.key.startsWith(part.toLowerCase())
);
break;
}
}
}
return currentProps.map((prop: CompletionProperty) => {
const name =
prop.key === '__default__'
? prefix.substr(0, prefix.length - 1)
: prefix + prop.key;
return { name, summary: prop.help };
});
};
export const getOffset = (el: HTMLElement) => {
const rect = el.getBoundingClientRect(),
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return { top: rect.top + scrollTop, left: rect.left + scrollLeft };
};
export const getVerticalScroll = (ele: Node) => {
let current = ele;
const verticalScroll = 0;
while (current) {
current = current.parentNode;
}
return verticalScroll;
};
export const getCompletionName = (option: CompletionOption): string => {
return (
option.name || option.signature.substr(0, option.signature.indexOf('('))
);
};
export const getCompletionSignature = (option: CompletionOption): string => {
return option.signature.substr(option.signature.indexOf('('));
};
/**
* Determines the pixel position of a character position inside an input element.
* For contenteditable elements, uses the Selection API directly.
* For textarea/input, uses a mirror-div technique.
*/
const getCursorXY = (input, selectionPoint) => {
// For contenteditable elements, use Selection API
if (input.isContentEditable) {
return getCursorXYContentEditable(input, selectionPoint);
}
const { offsetLeft: inputX, offsetTop: inputY } = input;
const div = document.createElement('div');
const copyStyle = getComputedStyle(input);
for (const prop of copyStyle) {
div.style[prop] = copyStyle[prop];
}
div.style.position = 'relative';
const swap = '.';
const inputValue =
input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value;
const textContent = inputValue.substr(0, selectionPoint);
div.textContent = textContent;
if (input.tagName === 'TEXTAREA') div.style.height = 'auto';
if (input.tagName === 'INPUT') div.style.width = 'auto';
const span = document.createElement('span');
span.textContent = inputValue.substr(selectionPoint) || '.';
div.appendChild(span);
document.body.appendChild(div);
const { offsetLeft: spanX, offsetTop: spanY } = span;
document.body.removeChild(div);
return {
left: inputX + spanX,
top: inputY + spanY
};
};
/**
* Finds the DOM position (node + offset) for a given plain-text offset.
* Newlines are \n characters inside text nodes (tok-newline spans).
* Browser-added <br> artifacts are ignored.
*/
const findDomPosition = (root, targetOffset) => {
let remaining = targetOffset;
const walk = (node) => {
if (node.nodeType === Node.TEXT_NODE) {
if (remaining <= node.textContent.length) {
return { node, offset: remaining };
}
remaining -= node.textContent.length;
return null;
}
// Ignore browser-added <br> artifacts
if (node.nodeName === 'BR') {
return null;
}
for (const child of node.childNodes) {
const result = walk(child);
if (result) return result;
}
return null;
};
return walk(root);
};
/**
* Gets cursor pixel position in a contenteditable element by creating a range
* at the target offset and measuring with getBoundingClientRect.
*/
const getCursorXYContentEditable = (element, offset) => {
// Save current selection
const sel = element.getRootNode() as ShadowRoot;
const selection = (sel as any).getSelection
? (sel as any).getSelection()
: window.getSelection();
const savedRange =
selection && selection.rangeCount > 0
? selection.getRangeAt(0).cloneRange()
: null;
const pos = findDomPosition(element, offset);
if (!pos) {
// Offset is beyond the text — use element bounds
const rect = element.getBoundingClientRect();
return { left: rect.left, top: rect.top };
}
// Create a range at the target position and measure it
const range = document.createRange();
range.setStart(pos.node, pos.offset);
range.collapse(true);
const rect = range.getBoundingClientRect();
// Restore original selection
if (savedRange && selection) {
selection.removeAllRanges();
selection.addRange(savedRange);
}
return {
left: rect.left - element.getBoundingClientRect().left + element.offsetLeft,
top: rect.top - element.getBoundingClientRect().top + element.offsetTop
};
};
export const updateInputElementWithCompletion = (
currentQuery: string,
ele: HTMLInputElement,
option: CompletionOption
) => {
let insertText = '';
if (option.signature) {
// they selected a function
insertText = option.signature.substr(0, option.signature.indexOf('(') + 1);
} else {
insertText = option.name;
}
const queryLength = currentQuery.length;
if (ele) {
const value = ele.value;
const insertionPoint = ele.selectionStart - queryLength;
// strip out our query
// const insertionPoint = value.lastIndexOf(value.substring(0, this.inputElement.selectionStart));
const leftSide = value.substr(0, insertionPoint);
const remaining = value.substr(insertionPoint + queryLength);
const caret = leftSide.length + insertText.length;
// set our value and our new caret
ele.value = leftSide + insertText + remaining;
ele.setSelectionRange(caret, caret);
// now scroll our text box if necessary
const position = getCursorXY(ele, caret);
if (position.left > ele.width) {
ele.scrollLeft = position.left;
}
ele.dispatchEvent(new Event('input'));
}
};
export const executeCompletionQuery = (
ele: HTMLInputElement,
session: boolean
): CompletionResult => {
const result: CompletionResult = {
currentFunction: null,
options: [],
anchorPosition: null,
query: null
};
if (!ele) {
return;
}
const store = getStore();
// we need a store to do anything useful
if (!store) {
return result;
}
const cursor = ele.selectionStart;
const fullValue = ele.value;
// Don't show completions when cursor is in the middle of a word
const charAfterCursor = fullValue[cursor];
if (charAfterCursor && /[\w.]/.test(charAfterCursor)) {
return result;
}
const input = fullValue.substring(0, cursor);
const parser = session ? sessionParser : messageParser;
const expressions = parser.findExpressions(input);
const currentExpression = expressions.find(
(expr: Expression) =>
expr.start <= cursor &&
(expr.end > cursor || (expr.end === cursor && !expr.closed))
);
if (currentExpression) {
const includeFunctions = currentExpression.text.indexOf('(') > -1;
if (includeFunctions) {
const functionQuery = parser.functionContext(currentExpression.text);
if (functionQuery) {
const fns = getFunctions(store.getFunctions(), functionQuery);
if (fns.length > 0) {
result.currentFunction = fns[0];
}
}
}
for (let i = currentExpression.text.length; i >= 0; i--) {
const curr = currentExpression.text[i];
if (
curr === '@' ||
curr === '(' ||
curr === ' ' ||
curr === ',' ||
curr === ')' ||
i === 0
) {
// don't include non-expression chars
if (
curr === '(' ||
curr === ' ' ||
curr === ',' ||
curr === ')' ||
curr === '@'
) {
i++;
}
const caret = getCursorXY(ele, currentExpression.start + i);
result.anchorPosition = {
left: caret.left - 2 - ele.scrollLeft,
top: caret.top - ele.scrollTop
};
result.query = currentExpression.text.substr(
i,
currentExpression.text.length - i
);
result.options = [
...getCompletions(
store.getCompletionSchema(),
result.query,
session,
store
),
...(includeFunctions
? getFunctions(store.getFunctions(), result.query)
: [])
];
// Don't show completion when the query is already an exact match
// for the only option (e.g. cursor at end of a complete function name)
if (result.options.length === 1) {
const opt = result.options[0];
const optName = (
opt.name ||
opt.signature?.substring(0, opt.signature.indexOf('(')) ||
''
).toLowerCase();
if (optName === result.query.toLowerCase()) {
result.options = [];
}
}
return result;
}
}
} else {
result.options = [];
result.query = '';
}
return result;
};
|