Skip to content

Commit

Permalink
refactor editor (#228)
Browse files Browse the repository at this point in the history
* remove opinionated extensions from default editor

* fix website and example

* add changeset
  • Loading branch information
xiaoxinghu authored Nov 27, 2023
1 parent 8a9c018 commit e9564ff
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-rings-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@orgajs/editor': patch
---

remove opinionated extensions from default editor
3 changes: 2 additions & 1 deletion examples/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"vite": "^4.4.9"
},
"dependencies": {
"@orgajs/editor": "workspace:^"
"@orgajs/editor": "workspace:^",
"cm6-theme-nord": "^0.2.0"
}
}
11 changes: 3 additions & 8 deletions examples/editor/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { EditorView } from '@codemirror/view'
import { nord } from 'cm6-theme-nord'
import { cleanup } from '@orgajs/editor/extensions'

export const theme = EditorView.theme({
'&': {
// background: 'gray',
},
})

const config = [theme]
const config = [nord, cleanup]

export default config
10 changes: 3 additions & 7 deletions packages/editor/lib/setup.js → packages/editor/lib/battery.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { foldGutter, toggleFold } from '@codemirror/language'
import { EditorView, highlightActiveLine, keymap } from '@codemirror/view'
import { org } from '@orgajs/cm-lang'
import { cleanupPlugin } from './plugins/cleanup.js'
import theme from './theme.js'
import { cleanup } from './extensions/index.js'

const keys = [
{
Expand All @@ -11,15 +9,13 @@ const keys = [
},
]

export const setup = (() => [
export const battery = (() => [
highlightActiveLine(),
foldGutter({
openText: '▾',
closedText: '▸',
}),
org(),
theme,
EditorView.lineWrapping,
cleanupPlugin,
cleanup,
keymap.of(keys),
])()
6 changes: 4 additions & 2 deletions packages/editor/lib/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* @property {OnChange} [onChange=() => {}]
*/
import { EditorView } from '@codemirror/view'
import { setup } from './setup.js'
import { Compartment, EditorState } from '@codemirror/state'
import { org } from '@orgajs/cm-lang'
import theme from './theme.js'

/**
* @param {Config} config
Expand All @@ -30,9 +31,10 @@ export function makeEditor(config) {
const state = EditorState.create({
doc: content,
extensions: [
org(),
themeConfig.of(EditorView.theme({}, { dark })),
theme,
extensions,
setup,
],
})
const editor = new EditorView({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function overlap(a, b) {
}

// TODO: this plugin is getting big, in terms of responsibility, break it down
export const cleanupPlugin = ViewPlugin.define(
const cleanup = ViewPlugin.define(
(view) => {
let _data = createDecorations(view)
let _selection = view.state.selection.main
Expand Down Expand Up @@ -66,7 +66,7 @@ export const cleanupPlugin = ViewPlugin.define(
})
},
},
},
}
)

/**
Expand Down Expand Up @@ -158,3 +158,5 @@ function createDecorations(view, decorations = Decoration.none) {
})
return { decorations, links }
}

export default cleanup
2 changes: 2 additions & 0 deletions packages/editor/lib/extensions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import cleanup from './cleanup.js'
export { cleanup }
2 changes: 1 addition & 1 deletion packages/editor/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*/

export { makeEditor } from './editor.js'
export { setup } from './setup.js'
export { battery } from './battery.js'
export { tags } from '@orgajs/cm-lang'
12 changes: 12 additions & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
"lib",
"dist"
],
"exports": {
".": {
"import": "./lib/index.js",
"require": "./lib/index.js",
"types": "./dist/index.d.ts"
},
"./extensions": {
"import": "./lib/extensions/index.js",
"require": "./lib/extensions/index.js",
"types": "./dist/extensions/index.d.ts"
}
},
"description": "react org-mode editor based on prose-mirror",
"scripts": {
"clean": "tsc --build --clean",
Expand Down
27 changes: 18 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion website/src/components/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EditorView } from '@codemirror/view'
import { makeEditor, type EditorConfig } from '@orgajs/editor'
import { makeEditor, battery, type EditorConfig } from '@orgajs/editor'
import { forwardRef, useEffect, useRef, useState } from 'react'
import { useMergeRefs } from '../utils/merge-refs'
import { setEditor } from './global'
Expand All @@ -16,6 +16,7 @@ const OrgEditor = forwardRef<HTMLDivElement, Partial<EditorConfig>>(
target: innerRef.current,
content: content,
onChange,
extensions: [battery],
})
setEditorView(editor)
setEditor(editor)
Expand Down

0 comments on commit e9564ff

Please sign in to comment.