Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 12,136 Bytes
8969f81 |
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 |
interface Datum {
id: string;
value: string;
}
export class Mention {
static Keys = {
TAB: 9,
ENTER: 13,
ESCAPE: 27,
UP: 38,
DOWN: 40,
};
static numberIsNaN = (x: any) => x !== x;
private isOpen = false;
/**
* index of currently selected item.
*/
private itemIndex = 0;
private mentionCharPos: number | undefined = undefined;
private cursorPos: number | undefined = undefined;
private values = [] as Datum[];
private suspendMouseEnter = false;
private options = {
source: (searchTerm: string, renderList: Function, mentionChar: string) => {},
renderItem: (item: Datum, searchTerm: string) => {
return `${item.value}`;
},
onSelect: (item: DOMStringMap, insertItem: (item: DOMStringMap) => void) => {
insertItem(item);
},
mentionDenotationChars: ['@'],
showDenotationChar: true,
allowedChars: /^[a-zA-Z0-9_]*$/,
minChars: 0,
maxChars: 31,
offsetTop: 2,
offsetLeft: 0,
/**
* Whether or not the denotation character(s) should be isolated. For example, to avoid mentioning in an email.
*/
isolateCharacter: false,
fixMentionsToQuill: false,
defaultMenuOrientation: 'bottom',
dataAttributes: ['id', 'value', 'denotationChar', 'link', 'target'],
linkTarget: '_blank',
onOpen: () => true,
onClose: () => true,
// Style options
listItemClass: 'ql-mention-list-item',
mentionContainerClass: 'ql-mention-list-container',
mentionListClass: 'ql-mention-list',
};
/// HTML elements
private mentionContainer = document.createElement('div');
private mentionList = document.createElement('ul');
constructor(
private quill: Quill,
) {
this.mentionContainer.className = this.options.mentionContainerClass;
this.mentionContainer.style.cssText = 'display: none; position: absolute;';
this.mentionContainer.onmousemove = this.onContainerMouseMove.bind(this);
if (this.options.fixMentionsToQuill) {
this.mentionContainer.style.width = 'auto';
}
this.mentionList.className = this.options.mentionListClass;
this.mentionContainer.appendChild(this.mentionList);
this.quill.container.appendChild(this.mentionContainer);
quill.on('text-change', this.onTextChange.bind(this));
quill.on('selection-change', this.onSelectionChange.bind(this));
quill.keyboard.addBinding({
key: Mention.Keys.ENTER,
}, this.selectHandler.bind(this));
quill.keyboard.bindings[Mention.Keys.ENTER].unshift(
quill.keyboard.bindings[Mention.Keys.ENTER].pop()
);
/// ^^ place it at beginning of bindings.
quill.keyboard.addBinding({
key: Mention.Keys.ESCAPE,
}, this.escapeHandler.bind(this));
quill.keyboard.addBinding({
key: Mention.Keys.UP,
}, this.upHandler.bind(this));
quill.keyboard.addBinding({
key: Mention.Keys.DOWN,
}, this.downHandler.bind(this));
document.addEventListener("keypress", e => {
/// Quick’n’dirty hack.
if (! this.quill.hasFocus()) {
return ;
}
setTimeout(() => {
this.setCursorPos();
this.quill.removeFormat(this.cursorPos! - 1, 1, 'silent');
}, 0);
});
}
selectHandler() {
if (this.isOpen) {
this.selectItem();
return false;
}
return true;
}
escapeHandler() {
if (this.isOpen) {
this.hideMentionList();
return false;
}
return true;
}
upHandler() {
if (this.isOpen) {
this.prevItem();
return false;
}
return true;
}
downHandler() {
if (this.isOpen) {
this.nextItem();
return false;
}
return true;
}
showMentionList() {
this.mentionContainer.style.visibility = 'hidden';
this.mentionContainer.style.display = '';
this.setMentionContainerPosition();
this.setIsOpen(true);
}
hideMentionList() {
this.mentionContainer.style.display = 'none';
this.setIsOpen(false);
}
private highlightItem(scrollItemInView = true) {
const childNodes = Array.from(this.mentionList.childNodes) as HTMLLIElement[];
for (const node of childNodes) {
node.classList.remove('selected');
}
childNodes[this.itemIndex].classList.add('selected');
if (scrollItemInView) {
const itemHeight = childNodes[this.itemIndex].offsetHeight;
const itemPos = this.itemIndex * itemHeight;
const containerTop = this.mentionContainer.scrollTop;
const containerBottom = containerTop + this.mentionContainer.offsetHeight;
if (itemPos < containerTop) {
// Scroll up if the item is above the top of the container
this.mentionContainer.scrollTop = itemPos;
} else if (itemPos > (containerBottom - itemHeight)) {
// scroll down if any part of the element is below the bottom of the container
this.mentionContainer.scrollTop += (itemPos - containerBottom) + itemHeight;
}
}
}
private getItemData(): DOMStringMap {
const node = this.mentionList.childNodes[this.itemIndex] as HTMLElement;
const { link } = node.dataset;
const itemTarget = node.dataset.target;
if (link !== undefined) {
node.dataset.value = `<a href="${link}" target=${itemTarget || this.options.linkTarget}>${node.dataset.value}`;
}
return node.dataset;
}
onContainerMouseMove() {
this.suspendMouseEnter = false;
}
selectItem() {
const data = this.getItemData();
this.options.onSelect(data, (asyncData) => {
this.insertItem(asyncData);
});
this.hideMentionList();
}
insertItem(data: DOMStringMap) {
const render = data;
if (render === null) {
return ;
}
if (!this.options.showDenotationChar) {
render.denotationChar = '';
}
if (this.cursorPos === undefined) {
throw new Error(`Invalid this.cursorPos`);
}
if (!render.value) {
throw new Error(`Didn't receive value from server.`);
}
this.quill.insertText(this.cursorPos, render.value, 'bold', Quill.sources.USER);
this.quill.setSelection(this.cursorPos + render.value.length, 0);
this.setCursorPos();
this.hideMentionList();
}
onItemMouseEnter(e: MouseEvent) {
if (this.suspendMouseEnter) {
return ;
}
const index = Number(
(e.target as HTMLLIElement).dataset.index
);
if (! Mention.numberIsNaN(index) && index !== this.itemIndex) {
this.itemIndex = index;
this.highlightItem(false);
}
}
onItemClick(e: MouseEvent) {
e.stopImmediatePropagation();
e.preventDefault();
this.itemIndex = Number(
(e.currentTarget as HTMLElement).dataset.index
);
this.highlightItem();
this.selectItem();
}
private attachDataValues(element: HTMLLIElement, data: Datum): HTMLLIElement {
for (const [key, value] of Object.entries(data)) {
if (this.options.dataAttributes.includes(key)) {
element.dataset[key] = value;
} else {
delete element.dataset[key];
}
}
return element;
}
renderList(mentionChar: string, data: Datum[], searchTerm: string = "") {
if (data.length > 0) {
this.values = data;
this.mentionList.innerHTML = '';
for (const [i, datum] of data.entries()) {
const li = document.createElement('li');
li.className = this.options.listItemClass;
li.dataset.index = `${i}`;
// li.innerHTML = this.options.renderItem(datum, searchTerm);
li.innerText = datum.value.replace(/\n/g, "↵");
/// ^^
li.onmouseenter = this.onItemMouseEnter.bind(this);
li.dataset.denotationChar = mentionChar;
li.onclick = this.onItemClick.bind(this);
this.mentionList.appendChild(
this.attachDataValues(li, datum)
);
}
this.itemIndex = 0;
this.highlightItem();
this.showMentionList();
} else {
this.hideMentionList();
}
}
nextItem() {
this.itemIndex = (this.itemIndex + 1) % this.values.length;
this.suspendMouseEnter = true;
this.highlightItem();
}
prevItem() {
this.itemIndex = ((this.itemIndex + this.values.length) - 1) % this.values.length;
this.suspendMouseEnter = true;
this.highlightItem();
}
private hasValidChars(s: string) {
return this.options.allowedChars.test(s);
}
private containerBottomIsNotVisible(topPos: number, containerPos: ClientRect | DOMRect) {
const mentionContainerBottom = topPos + this.mentionContainer.offsetHeight + containerPos.top;
return mentionContainerBottom > window.pageYOffset + window.innerHeight;
}
private containerRightIsNotVisible(leftPos: number, containerPos: ClientRect | DOMRect) {
if (this.options.fixMentionsToQuill) {
return false;
}
const rightPos = leftPos + this.mentionContainer.offsetWidth + containerPos.left;
const browserWidth = window.pageXOffset + document.documentElement.clientWidth;
return rightPos > browserWidth;
}
private setIsOpen(isOpen: boolean) {
if (this.isOpen !== isOpen) {
if (isOpen) {
this.options.onOpen();
} else {
this.options.onClose();
}
this.isOpen = isOpen;
}
}
private setMentionContainerPosition() {
const containerPos = this.quill.container.getBoundingClientRect();
/// vv Here we always trigger from the cursor.
if (this.cursorPos === undefined) {
throw new Error(`Invalid this.cursorPos`);
}
const mentionCharPos = this.quill.getBounds(this.cursorPos);
const containerHeight = this.mentionContainer.offsetHeight;
let topPos = this.options.offsetTop;
let leftPos = this.options.offsetLeft;
// handle horizontal positioning
if (this.options.fixMentionsToQuill) {
const rightPos = 0;
this.mentionContainer.style.right = `${rightPos}px`;
} else {
leftPos += mentionCharPos.left;
}
if (this.containerRightIsNotVisible(leftPos, containerPos)) {
const containerWidth = this.mentionContainer.offsetWidth + this.options.offsetLeft;
const quillWidth = containerPos.width;
leftPos = quillWidth - containerWidth;
}
// handle vertical positioning
if (this.options.defaultMenuOrientation === 'top') {
// Attempt to align the mention container with the top of the quill editor
if (this.options.fixMentionsToQuill) {
topPos = -1 * (containerHeight + this.options.offsetTop);
} else {
topPos = mentionCharPos.top - (containerHeight + this.options.offsetTop);
}
// default to bottom if the top is not visible
if (topPos + containerPos.top <= 0) {
let overMentionCharPos = this.options.offsetTop;
if (this.options.fixMentionsToQuill) {
overMentionCharPos += containerPos.height;
} else {
overMentionCharPos += mentionCharPos.bottom;
}
topPos = overMentionCharPos;
}
} else {
// Attempt to align the mention container with the bottom of the quill editor
if (this.options.fixMentionsToQuill) {
topPos += containerPos.height;
} else {
topPos += mentionCharPos.bottom;
}
// default to the top if the bottom is not visible
if (this.containerBottomIsNotVisible(topPos, containerPos)) {
let overMentionCharPos = this.options.offsetTop * -1;
if (!this.options.fixMentionsToQuill) {
overMentionCharPos += mentionCharPos.top;
}
topPos = overMentionCharPos - containerHeight;
}
}
this.mentionContainer.style.top = `${topPos}px`;
this.mentionContainer.style.left = `${leftPos}px`;
this.mentionContainer.style.visibility = 'visible';
}
/**
* HF Helpers for manual trigger
*/
setCursorPos() {
const range = this.quill.getSelection();
if (range) {
this.cursorPos = range.index;
} else {
this.quill.setSelection(this.quill.getLength(), 0);
/// ^^ place cursor at the end of input by default.
this.cursorPos = this.quill.getLength();
}
}
getCursorPos(): number {
return this.cursorPos!;
}
trigger(values: string[]) {
this.renderList("", values.map(x => {
return { id: x, value: x };
}), "");
}
onSomethingChange() {
/// We trigger manually so here we can _probably_ just always close.
this.hideMentionList();
}
onTextChange(delta: Delta, oldDelta: Delta, source: Sources) {
if (source === 'user') {
this.onSomethingChange();
}
}
onSelectionChange(range: RangeStatic) {
if (range && range.length === 0) {
this.onSomethingChange();
} else {
this.hideMentionList();
}
}
}
Quill.register('modules/mention', Mention);
|