Skip to content
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
42 changes: 42 additions & 0 deletions core/src/theme/dimensionTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { EditorView } from '@codemirror/view';
import type { Extension } from '@codemirror/state';

export const scrollerTheme = EditorView.theme({
'& .cm-scroller': {
height: '100% !important',
},
});

let lastDimensionKey: string | null = null;
let lastDimensionTheme: Extension | null = null;

export function getDimensionTheme(
height: string | null,
minHeight: string | null,
maxHeight: string | null,
width: string | null,
minWidth: string | null,
maxWidth: string | null,
): Extension | null {
if (!height && !minHeight && !maxHeight && !width && !minWidth && !maxWidth) {
return null;
}

const cacheKey = JSON.stringify({ height, minHeight, maxHeight, width, minWidth, maxWidth });
if (cacheKey === lastDimensionKey) {
return lastDimensionTheme;
}

lastDimensionKey = cacheKey;
lastDimensionTheme = EditorView.theme({
'&': {
height,
minHeight,
maxHeight,
width,
minWidth,
maxWidth,
},
});
return lastDimensionTheme;
}
22 changes: 8 additions & 14 deletions core/src/useCodeMirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getDefaultExtensions } from './getDefaultExtensions';
import { getStatistics } from './utils';
import { type ReactCodeMirrorProps } from '.';
import { TimeoutLatch, getScheduler } from './timeoutLatch';
import { scrollerTheme, getDimensionTheme } from './theme/dimensionTheme';

export const ExternalChange = Annotation.define<boolean>();
const TYPING_TIMOUT = 200; // ms
Expand Down Expand Up @@ -45,19 +46,7 @@ export function useCodeMirror(props: UseCodeMirror) {
const [state, setState] = useState<EditorState>();
const typingLatch = useState<{ current: TimeoutLatch | null }>(() => ({ current: null }))[0];
const pendingUpdate = useState<{ current: (() => void) | null }>(() => ({ current: null }))[0];
const defaultThemeOption = EditorView.theme({
'&': {
height,
minHeight,
maxHeight,
width,
minWidth,
maxWidth,
},
'& .cm-scroller': {
height: '100% !important',
},
});
const defaultThemeOption = getDimensionTheme(height, minHeight, maxHeight, width, minWidth, maxWidth);
const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {
if (
vu.docChanged &&
Expand Down Expand Up @@ -96,7 +85,12 @@ export function useCodeMirror(props: UseCodeMirror) {
basicSetup: defaultBasicSetup,
});

let getExtensions = [updateListener, defaultThemeOption, ...defaultExtensions];
let getExtensions = [
updateListener,
...(defaultThemeOption ? [defaultThemeOption] : []),
scrollerTheme,
...defaultExtensions,
];

if (onUpdate && typeof onUpdate === 'function') {
getExtensions.push(EditorView.updateListener.of(onUpdate));
Expand Down