Skip to content

Commit

Permalink
Improve scrolling behavior for typewriter mode (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 18, 2025
1 parent a0c1639 commit 668e5aa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CoreEditor/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function resetEditor(initialContent: string) {
setTimeout(ensureLineHeight, 600);

// Makes sure the content doesn't have unwanted inset
scrollIntoView(0);
scrollIntoView(0, window.config.typewriterMode ? 'center' : undefined);

const contentDOM = editor.contentDOM;
contentDOM.addEventListener('blur', handleFocusLost);
Expand Down
5 changes: 2 additions & 3 deletions CoreEditor/src/modules/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ export function setReadOnlyMode(enabled: boolean) {

export function setTypewriterMode(enabled: boolean) {
window.config.typewriterMode = enabled;
styling.setTypewriterMode(enabled);

if (enabled) {
scrollToSelection('center');
}
scrollToSelection(enabled ? 'center' : 'nearest');
}

export function setFocusMode(enabled: boolean) {
Expand Down
14 changes: 14 additions & 0 deletions CoreEditor/src/styling/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default interface StyleSheets {
accentColor?: HTMLStyleElement;
fontFace?: HTMLStyleElement;
fontSize?: HTMLStyleElement;
typewriterMode?: HTMLStyleElement;
focusMode?: HTMLStyleElement;
lineHeight?: HTMLStyleElement;
taskMarker?: HTMLStyleElement;
Expand All @@ -32,6 +33,7 @@ export function setUp(config: Config, colors: EditorColors) {
setFontFace(config.fontFace);
setFontSize(config.fontSize);
setInvisiblesBehavior(config.invisiblesBehavior);
setTypewriterMode(config.typewriterMode);
setFocusMode(config.focusMode);
setLineHeight(config.lineHeight);

Expand Down Expand Up @@ -157,6 +159,18 @@ export function setInvisiblesBehavior(behavior: InvisiblesBehavior) {
}
}

export function setTypewriterMode(enabled: boolean) {
if (styleSheets.typewriterMode === undefined) {
styleSheets.typewriterMode = createStyleSheet(`
.cm-content {
padding-top: 50vh !important;
}
`, false);
}

styleSheets.typewriterMode.disabled = !enabled;
}

export function setFocusMode(enabled: boolean) {
const editor = window.editor as EditorView | null;
if (typeof editor?.dispatch === 'function') {
Expand Down

0 comments on commit 668e5aa

Please sign in to comment.