All files / src getDefaultExtensions.ts

79.16% Statements 19/24
43.47% Branches 10/23
100% Functions 1/1
79.16% Lines 19/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                        1x               31x 31x 31x 31x   31x 31x 31x         31x 4x   31x   29x 29x   2x 2x             31x 4x   31x       31x    
import { type Extension } from '@codemirror/state';
import { indentWithTab } from '@codemirror/commands';
import { basicSetup, type BasicSetupOptions } from '@uiw/codemirror-extensions-basic-setup';
import { EditorView, keymap, placeholder } from '@codemirror/view';
import { oneDark } from '@codemirror/theme-one-dark';
import { EditorState } from '@codemirror/state';
import { defaultLightThemeOption } from './theme/light';
 
export * from '@codemirror/theme-one-dark';
export * from './theme/light';
 
export interface DefaultExtensionsOptions {
  indentWithTab?: boolean;
  basicSetup?: boolean | BasicSetupOptions;
  placeholder?: string | HTMLElement;
  theme?: 'light' | 'dark' | 'none' | Extension;
  readOnly?: boolean;
  editable?: boolean;
}
 
export const getDefaultExtensions = (optios: DefaultExtensionsOptions = {}): Extension[] => {
  const {
    indentWithTab: defaultIndentWithTab = true,
    editable = true,
    readOnly = false,
    theme = 'light',
    placeholder: placeholderStr = '',
    basicSetup: defaultBasicSetup = true,
  } = optios;
  const getExtensions: Extension[] = [];
  Eif (defaultIndentWithTab) {
    getExtensions.unshift(keymap.of([indentWithTab]));
  }
  Eif (defaultBasicSetup) {
    if (typeof defaultBasicSetup === 'boolean') {
      getExtensions.unshift(basicSetup());
    } else E{
      getExtensions.unshift(basicSetup(defaultBasicSetup));
    }
  }
  if (placeholderStr) {
    getExtensions.unshift(placeholder(placeholderStr));
  }
  switch (theme) {
    case 'light':
      getExtensions.push(defaultLightThemeOption);
      break;
    case 'dark':
      getExtensions.push(oneDark);
      break;
    case 'none':
      break;
    default:
      getExtensions.push(theme);
      break;
  }
  if (editable === false) {
    getExtensions.push(EditorView.editable.of(false));
  }
  Iif (readOnly) {
    getExtensions.push(EditorState.readOnly.of(true));
  }
 
  return [...getExtensions];
};