Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose the scroll height #36

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ <h3 align="center">A small but feature-rich code editor for the web</h3>
parent.appendChild(element);

const editor = KullnaEditor.createEditor(element, options);

const highlight = editor.createHighlight();
highlight.lineNumber = 0;
highlight.cssClass = 'code-gutter-highlight';
highlight.visible = true;

editor.onSelectionFocusChanged(document => {
editor.highlightedLine = document.currentLineNumber;
highlight.lineNumber = document.currentLineNumber;
});
editor.code = defaultValue;

Expand All @@ -210,7 +216,7 @@ <h3 align="center">A small but feature-rich code editor for the web</h3>
const standard = demoWithOptions(
'#demos',
'standard',
'Standard Configuration',
'Standard Configuration (Auto-Sizing)',
{
language: 'html',
highlightElement: e => {
Expand Down Expand Up @@ -252,6 +258,10 @@ <h3 align="center">A small but feature-rich code editor for the web</h3>
standardHighlight.lineNumber = 4;
standardHighlight.visible = true;
standardHighlight.cssClass = 'breakpoint-highlight';
standard.onUpdate(code => {
const element = document.getElementById('editor-standard');
element.style.height = standard.naturalHeight;
});

demoWithOptions(
'#demos',
Expand Down
8 changes: 8 additions & 0 deletions src/internals/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export class Editor
}
}

/** @inheritDoc */
get naturalHeight(): string {
return this.view.naturalHeight;
}

/** @inheritDoc */
scrollToLine(line: number): void {
this.view.scrollToLine(line);
Expand Down Expand Up @@ -361,6 +366,9 @@ export class Editor
if (this.options.onUpdate) {
this.options.onUpdate(this.code);
}
if (this.options.onSelectionFocusChanged) {
this.options.onSelectionFocusChanged(this.view.document);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/internals/text_editor/dom/dom_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export class DomBridge {
if (document.perceptuallyEquals(oldDocument)) {
this._onDocumentSelectionChangedCallbacks.forEach(callback => callback(document));
} else {
this._onDocumentContentAndSelectionChangedCallbacks.forEach(callback => callback(document));
this.recalculateLineMetrics();
this._onDocumentContentAndSelectionChangedCallbacks.forEach(callback => callback(document));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/internals/text_editor/text_editor_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ export class TextEditorView {
this._bridge.pushToDOM(document);
}

/** @inheritDoc */
get naturalHeight(): string {
const lastLine = this.lineMetrics[this.lineMetrics.length - 1];
const bottomContent = lastLine.top + lastLine.height;
let bottomPadding = getComputedStyle(this.contentEditableSurface).paddingBottom;
if (!bottomPadding || bottomPadding.length === 0) {
bottomPadding = '0px';
}
return `calc(${bottomContent}px + ${bottomPadding})`;
}

/**
* Template pattern for adding an event listener to the DOM and our list.
*
Expand Down
8 changes: 8 additions & 0 deletions src/kullna_editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ export interface KullnaEditor {
get dir(): string;
set dir(dir: string);

/**
* Gets the total height of the editor's content as a CSS value such as `123px` or `min(x, y)`.
*
* @remarks
* Intended to be queried as part of an `onUpdate` implementation.
*/
get naturalHeight(): string;

/**
* Ensures a specific line number is within view.
*
Expand Down
Loading