透過 100+ 個技巧學習 Nuxt!

nuxt-codemirror
nuxt-codemirror

Nuxt codemirror 模組

Nuxt CodeMirror

npm version npm downloads License Nuxt

Codemirror 作為 Nuxt 模組。範例可以在下面的線上遊樂場找到

功能

  • 🚀 使用幾乎所有 API 輕鬆配置 codemirror 以滿足您的需求
  • 🚠 使用 Typescript 構建
  • 🌲 自訂 useNuxtCodeMirror composable 以建立您自己的編輯器
  • 為 CodeMirror 6 及以上版本構建

文件

此模組包含一個組件、一個 Composable 和一些供您使用的類型。請閱讀 CodeMirror 參考指南以獲取更多深入資訊:CodeMirror 文件

NuxtCodeMirror.vue

此組件會自動匯入,並且是 CodeMirror 的包裝器。

組件 props

interface NuxtCodeMirrorProps
  extends Omit<EditorStateConfig, 'doc' | 'extensions'> {
  /** value of the auto created model in the editor. Works as underlying logic of a V-Model */
  modelValue?: string
  /** The height value of the editor. */
  height?: string
  /** The minimum height value of the editor. */
  minHeight?: string
  /** The maximum height value of the editor. */
  maxHeight?: string
  /** The width value of the editor. */
  width?: string
  /** The minimum width value of the editor. */
  minWidth?: string
  /** The maximum width value of the editor. */
  maxWidth?: string
  /** focus on the editor. */
  autoFocus?: boolean
  /** Enables a placeholder—a piece of example content to show when the editor is empty. */
  placeholder?: string | HTMLElement
  /**
   * `light` / `dark` / `Extension` Defaults to `light`.
   * @default light
   */
  theme?: 'light' | 'dark' | 'none' | Extension
  /**
   * Whether to optional basicSetup by default
   * @default true
   */
  basicSetup?: boolean | BasicSetupOptions
  /**
   * This disables editing of the editor content by the user.
   * @default true
   */
  editable?: boolean
  /**
   * This disables editing of the editor content by the user.
   * @default false
   */
  readOnly?: boolean
  /**
   * Controls whether pressing the `Tab` key inserts a tab character and indents the text (`true`)
   * or behaves according to the browser's default behavior (`false`).
   * @default true
   */
  indentWithTab?: boolean
  /** Fired whenever a change occurs to the document. */
  onChange?(value: string, viewUpdate: ViewUpdate): void
  /** Some data on the statistics editor. */
  onStatistics?(data: Statistics): void
  /** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */
  onUpdate?(viewUpdate: ViewUpdate): void
  /** The first time the editor executes the event. */
  onCreateEditor?(view: EditorView, state: EditorState): void
  /** Fired whenever the editor is focused. */
  onFocus?(view: ViewUpdate): void
  /** Fired whenever the editor is blurred. */
  onBlur?(view: ViewUpdate): void
  /**
   * Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.
   * They can either be built-in extension-providing objects,
   * such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of),
   * or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.
   */
  extensions?: Extension[]
  /**
   * If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.
   * Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)
   */
  root?: ShadowRoot | Document
  /**
   * Create a state from its JSON representation serialized with [toJSON](https://codemirror.net/docs/ref/#state.EditorState.toJSON) function
   */
  initialState?: {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    json: any
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    fields?: Record<string, StateField<any>>
  }
}

NuxtCodeMirror 組件接受一個 Template ref 並公開 CodeMirror div 元素、Editor 視圖和 Editor 狀態

interface CodeMirrorRef {
  /** Container element of the CodeMirror instance */
  container: HTMLDivElement | null
  /** The EditorView of the CodeMirror instance */
  view: EditorView | undefined
  /** The EditorState of the CodeMirror instance */
  state: EditorState | undefined
  /** Editor element of the CodeMirror instance */
  editor: HTMLDivElement | null
}

如果您需要存取底層的 CodeMirror 實例,您可以透過新增一個與您的 Template ref 同名的 ref 來執行此操作。 如何執行此操作的範例可以在這裡找到:🏀 線上遊樂場

如果因為某些原因您想編寫自己的 CodeMirror 組件,您也可以這麼做 :)

UseNuxtCodeMirror.ts

此 composable 是 NuxtCodeMirror 組件的底層魔法,也會自動匯入。

它至少需要 3 個 Ref,一個用於 CodeMirror 將連接的 div 元素,一個用於 CodeMirror 視圖,另一個用於 CodeMirror 狀態

請確保您在 onMounted 中執行 composable,否則您會收到錯誤,因為 codemirror 無法連結到 DOM。

const editor = ref<HTMLDivElement | null>(null)
const container = ref<HTMLDivElement | null>(null)
const view = ref<EditorView>()
const state = ref<EditorState>()

onMounted(() => {
  useNuxtCodeMirror({
    ...props,
    container: editor.value,
    viewRef: view,
    stateRef: state,
    containerRef: container,
  })
})

有關如何實作的更多資訊,請參閱原始碼,以獲取您自己版本的靈感

致謝

這個 Nuxt 模組的實現離不開

快速設定

使用一個命令將模組安裝到您的 Nuxt 應用程式

npx nuxi module add nuxt-codemirror

就這樣!您現在可以在您的 Nuxt 應用程式中使用 Nuxt-codemirror ✨

已測試的擴充功能

以下是截至 @codemirror/view 版本 6.29.1 和 @codemirror/state 版本 6.4.1 您可以使用的已測試擴充功能清單

等等更多...

貢獻

如果您有任何想法或錯誤,請隨時開啟一個 issue。對於想法,請開始討論。

我歡迎任何形式的貢獻,提前感謝您!!

本地開發
# Install dependencies
pnpm i

# Generate type stubs
pnpm dev:prepare

# Develop with the playground
pnpm dev

# Build the playground
pnpm dev:build

# Run ESLint
pnpm lint

# Run Vitest
pnpm test
pnpm test:watch

# Release new version
pnpm release

常見問題

問題

  • 在使用您的模組啟動開發伺服器時,我收到錯誤:Pre-transform error: Failed to resolve import "@codemirror/view", Pre-transform error: Failed to resolve import "@codemirror/state"

解決方案

  • 假設您將 pnpm 與 shamefully-hoist=false 搭配使用,請安裝 @codemirror/state@codemirror/view,這些錯誤應該會消失