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 | 92x 95x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 89x 89x 89x 89x 89x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 92x 92x 92x 92x 92x 12x 12x 12x 12x 12x 11x 11x 11x 92x 3x 3x 1x 1x 3x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 89x 89x 89x 89x 89x 89x 89x 92x 92x 92x | import { TemplateResult, css, html } from 'lit';
import { property } from 'lit/decorators.js';
import { ifDefined } from 'lit-html/directives/if-defined.js';
import { TextInput } from './TextInput';
import {
renderCompletionOption,
updateInputElementWithCompletion,
executeCompletionQuery
} from '../excellent/helpers';
import { FieldElement } from './FieldElement';
import { CompletionOption, Position } from '../interfaces';
import { styleMap } from 'lit-html/directives/style-map.js';
import { msg } from '@lit/localize';
/**
* Completion is a text input that handles excellent completion options in a popup
*/
export class Completion extends FieldElement {
static get styles() {
return css`
${super.styles}
:host {
display: block;
}
temba-options {
--widget-box-shadow-focused: 0 0 4px rgba(0, 0, 0, 0.15);
--color-focus: #e6e6e6;
}
.comp-container {
position: relative;
height: 100%;
}
#anchor {
/* background: rgba(132, 40, 158, .1); */
position: absolute;
visibility: hidden;
width: 250px;
height: 20px;
}
.fn-marker {
font-weight: bold;
font-size: 42px;
}
.option-slot {
background: #fff;
}
.current-fn {
padding: 10px;
margin: 5px;
background: var(--color-primary-light);
color: rgba(0, 0, 0, 0.5);
border-radius: var(--curvature-widget);
font-size: 90%;
}
.footer {
padding: 5px 10px;
background: var(--color-primary-light);
color: rgba(0, 0, 0, 0.5);
font-size: 80%;
border-bottom-left-radius: var(--curvature-widget);
border-bottom-right-radius: var(--curvature-widget);
}
code {
padding: 1px 5px;
border-radius: var(--curvature);
}
`;
}
@property({ type: Number })
maxLength: number;
@property({ type: Boolean })
session: boolean;
@property({ type: Boolean })
submitOnEnter = false;
@property({ type: Object })
anchorPosition: Position = { left: 0, top: 0 };
@property({ attribute: false })
currentFunction: CompletionOption;
@property({ type: String })
placeholder = '';
textInputElement: TextInput;
anchorElement: HTMLDivElement;
@property({ type: Array })
options: any[] = [];
@property({ type: String })
name = '';
@property({ type: Boolean })
textarea: boolean;
@property({ type: Boolean })
gsm: boolean;
@property({ type: Boolean })
disableCompletion: boolean;
@property({ type: String })
counter: string;
@property({ type: Boolean })
autogrow = false;
@property({ type: Number })
minHeight: number;
private hiddenElement: HTMLInputElement;
private query: string;
public firstUpdated() {
this.textInputElement = this.shadowRoot.querySelector(
'temba-textinput'
) as TextInput;
this.anchorElement = this.shadowRoot.querySelector('#anchor');
// create our hidden container so it gets included in our host element's form
this.hiddenElement = document.createElement('input');
this.hiddenElement.setAttribute('type', 'hidden');
this.hiddenElement.setAttribute('name', this.getAttribute('name'));
this.hiddenElement.setAttribute('value', this.getAttribute('value') || '');
this.appendChild(this.hiddenElement);
}
private handleKeyUp(evt: KeyboardEvent) {
if (this.disableCompletion) {
// if we have options, ignore keys that are meant for them
if (this.options && this.options.length > 0) {
if (evt.key === 'ArrowUp' || evt.key === 'ArrowDown') {
return;
}
if (evt.ctrlKey) {
if (evt.key === 'n' || evt.key === 'p') {
return;
}
}
if (
evt.key === 'Enter' ||
evt.key === 'Escape' ||
evt.key === 'Tab' ||
evt.key.startsWith('Control')
) {
evt.stopPropagation();
evt.preventDefault();
return;
}
this.executeQuery(evt.currentTarget as TextInput);
}
}
}
public hasVisibleOptions() {
return this.options.length > 0;
}
private executeQuery(ele: TextInput) {
if (this.disableCompletion) {
return;
}
if (!ele.inputElement) {
return;
}
const result = executeCompletionQuery(ele.inputElement, this.session);
this.query = result.query;
this.options = result.options;
this.anchorPosition = result.anchorPosition;
}
private handleClick(evt: MouseEvent) {
this.executeQuery(evt.currentTarget as TextInput);
}
public updated(changedProperties: Map<string, any>) {
super.updated(changedProperties);
// if our cursor changed, lets make sure our scrollbox is showing it
if (changedProperties.has('value')) {
this.hiddenElement.setAttribute('value', this.value);
}
}
private handleInput(evt: KeyboardEvent) {
const ele = evt.currentTarget as TextInput;
this.executeQuery(ele);
this.value = ele.inputElement.value;
this.fireEvent('change');
}
private handleOptionCanceled() {
// delay in case we are actively selecting
window.setTimeout(() => {
this.options = [];
this.query = '';
}, 100);
}
private handleOptionSelection(evt: CustomEvent) {
const option = evt.detail.selected as CompletionOption;
const tabbed = evt.detail.tabbed;
updateInputElementWithCompletion(
this.query,
this.textInputElement.inputElement,
option
);
this.query = '';
this.options = [];
if (tabbed) {
this.executeQuery(this.textInputElement);
}
}
public getTextInput(): TextInput {
return this.textInputElement;
}
public click() {
super.click();
const input = this.shadowRoot.querySelector('temba-textinput') as TextInput;
if (input) {
input.click();
}
}
public focus() {
super.focus();
const input = this.shadowRoot.querySelector('temba-textinput') as TextInput;
if (input) {
input.focus();
}
}
protected renderWidget(): TemplateResult {
const anchorStyles = this.anchorPosition
? {
top: `${this.anchorPosition.top}px`,
left: `${this.anchorPosition.left}px`
}
: {};
const visible = this.options && this.options.length > 0;
return html`
<div class="comp-container">
<div id="anchor" style=${styleMap(anchorStyles)}></div>
<temba-textinput
name=${this.name}
placeholder=${this.placeholder}
gsm=${this.gsm}
counter=${ifDefined(this.counter)}
@keyup=${this.handleKeyUp}
@click=${this.handleClick}
@input=${this.handleInput}
@blur=${this.handleOptionCanceled}
maxlength="${ifDefined(this.maxLength)}"
.value=${this.value}
?autogrow=${this.autogrow}
?textarea=${this.textarea}
?submitOnEnter=${this.submitOnEnter}
style=${this.minHeight
? `--textarea-min-height: ${this.minHeight}px`
: ''}
>
</temba-textinput>
<temba-options
@temba-selection=${this.handleOptionSelection}
@temba-canceled=${this.handleOptionCanceled}
.renderOption=${renderCompletionOption}
.anchorTo=${this.anchorElement}
.options=${this.options}
?visible=${visible}
>
${this.currentFunction
? html`
<div class="current-fn">
${renderCompletionOption(this.currentFunction, true)}
</div>
`
: null}
<div class="footer" style="${!visible ? 'display:none' : null}">
${msg('Tab to complete, enter to select')}
</div>
</temba-options>
</div>
`;
}
public render(): TemplateResult {
return this.renderField();
}
}
|