|
6 | 6 | * **<https://github.com/WebCoder49/code-input>** |
7 | 7 | */ |
8 | 8 |
|
9 | | - |
10 | 9 | var codeInput = { |
11 | 10 | /** |
12 | 11 | * A list of attributes that will trigger the |
@@ -64,7 +63,8 @@ var codeInput = { |
64 | 63 | "change", |
65 | 64 | "selectionchange", |
66 | 65 | "invalid", |
67 | | - "input" |
| 66 | + "input", |
| 67 | + "scroll" |
68 | 68 | ], |
69 | 69 |
|
70 | 70 | /* ------------------------------------ |
@@ -512,27 +512,9 @@ var codeInput = { |
512 | 512 | if (this.template.includeCodeInputInHighlightFunc) this.template.highlight(resultElement, this); |
513 | 513 | else this.template.highlight(resultElement); |
514 | 514 |
|
515 | | - this.syncSize(); |
516 | | - |
517 | 515 | this.pluginEvt("afterHighlight"); |
518 | 516 | } |
519 | 517 |
|
520 | | - /** |
521 | | - * Set the size of the textarea element to the size of the pre/code element. |
522 | | - */ |
523 | | - syncSize() { |
524 | | - // Synchronise the size of the pre/code and textarea elements |
525 | | - if(this.template.preElementStyled) { |
526 | | - this.style.backgroundColor = getComputedStyle(this.preElement).backgroundColor; |
527 | | - this.textareaElement.style.height = getComputedStyle(this.preElement).height; |
528 | | - this.textareaElement.style.width = getComputedStyle(this.preElement).width; |
529 | | - } else { |
530 | | - this.style.backgroundColor = getComputedStyle(this.codeElement).backgroundColor; |
531 | | - this.textareaElement.style.height = getComputedStyle(this.codeElement).height; |
532 | | - this.textareaElement.style.width = getComputedStyle(this.codeElement).width; |
533 | | - } |
534 | | - } |
535 | | - |
536 | 518 | /** |
537 | 519 | * Show some instructions to the user only if they are using keyboard navigation - for example, a prompt on how to navigate with the keyboard if Tab is repurposed. |
538 | 520 | * @param {string} instructions The instructions to display only if keyboard navigation is being used. If it's blank, no instructions will be shown. |
@@ -684,22 +666,64 @@ var codeInput = { |
684 | 666 | this.value = value; |
685 | 667 | this.animateFrame(); |
686 | 668 |
|
687 | | - const resizeObserver = new ResizeObserver((elements) => { |
688 | | - // The only element that could be resized is this code-input element. |
689 | | - this.syncSize(); |
| 669 | + // Scrolling |
| 670 | + this.textareaElement.addEventListener("scroll", () => { |
| 671 | + this.syncScrollFromTextarea(); |
690 | 672 | }); |
691 | | - resizeObserver.observe(this); |
| 673 | + if(this.template.preElementStyled) { |
| 674 | + this.preElement.addEventListener("scroll", () => { |
| 675 | + this.syncScrollFromHighlighted(this.preElement) |
| 676 | + }); |
| 677 | + } else { |
| 678 | + this.codeElement.addEventListener("scroll", () => { |
| 679 | + this.syncScrollFromHighlighted(this.codeElement) |
| 680 | + }); |
| 681 | + } |
| 682 | + // TODO: Make scrollTop etc. directly accessible from code-input. |
| 683 | + } |
| 684 | + |
| 685 | + /** |
| 686 | + * Synchronise the scroll position of the textarea element to that of the pre/code element. |
| 687 | + * @param {HTMLElement} highlightedElement The pre/code element that has been scrolled (the styled one). |
| 688 | + */ |
| 689 | + syncScrollFromHighlighted(highlightedElement) { |
| 690 | + this.textareaElement.scrollTo(highlightedElement.scrollLeft, highlightedElement.scrollTop); |
| 691 | + } |
| 692 | + |
| 693 | + /** |
| 694 | + * Synchronise the scroll position of the pre/code element to that of the textarea element. |
| 695 | + */ |
| 696 | + syncScrollFromTextarea() { |
| 697 | + if(this.template.preElementStyled) { |
| 698 | + this.preElement.scrollTo(this.textareaElement.scrollLeft, this.textareaElement.scrollTop); |
| 699 | + } else { |
| 700 | + this.codeElement.scrollTo(this.textareaElement.scrollLeft, this.textareaElement.scrollTop); |
| 701 | + } |
| 702 | + } |
| 703 | + |
| 704 | + /** |
| 705 | + * @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made as clear in the past. |
| 706 | + */ |
| 707 | + syncScroll() { |
| 708 | + this.syncScrollFromTextarea(); |
| 709 | + } |
| 710 | + |
| 711 | + /** |
| 712 | + * @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made as clear in the past, and will remain until the next major version of code-inp. |
| 713 | + */ |
| 714 | + sync_scroll() { |
| 715 | + this.syncScroll(); |
692 | 716 | } |
693 | 717 |
|
694 | 718 | /** |
695 | | - * @deprecated Please use `codeInput.CodeInput.escapeHtml` |
| 719 | + * @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made clear in the past. |
696 | 720 | */ |
697 | 721 | escape_html(text) { |
698 | 722 | return this.escapeHtml(text); |
699 | 723 | } |
700 | 724 |
|
701 | 725 | /** |
702 | | - * @deprecated Please use `codeInput.CodeInput.getTemplate` |
| 726 | + * @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made clear in the past. |
703 | 727 | */ |
704 | 728 | get_template() { |
705 | 729 | return this.getTemplate(); |
|
0 commit comments