-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add react-codemirror-merge component (#455).
- Loading branch information
1 parent
83f1d1f
commit ba3d076
Showing
21 changed files
with
515 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Extension } from '@codemirror/state'; | ||
import { indentWithTab } from '@codemirror/commands'; | ||
import { basicSetup, 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'; | ||
|
||
export type 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[] = []; | ||
const defaultLightThemeOption = EditorView.theme( | ||
{ | ||
'&': { | ||
backgroundColor: '#fff', | ||
}, | ||
}, | ||
{ | ||
dark: false, | ||
}, | ||
); | ||
if (defaultIndentWithTab) { | ||
getExtensions.unshift(keymap.of([indentWithTab])); | ||
} | ||
if (defaultBasicSetup) { | ||
if (typeof defaultBasicSetup === 'boolean') { | ||
getExtensions.unshift(basicSetup()); | ||
} else { | ||
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)); | ||
} | ||
if (readOnly) { | ||
getExtensions.push(EditorState.readOnly.of(true)); | ||
} | ||
|
||
return [...getExtensions]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "4.19.11", | ||
"packages": ["themes/**", "core", "www"], | ||
"packages": ["themes/**", "core", "merge", "www"], | ||
"useWorkspaces": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!--rehype:ignore:start--> | ||
|
||
# react-codemirror-merge | ||
|
||
<!--rehype:ignore:end--> | ||
|
||
[![npm version](https://img.shields.io/npm/v/react-codemirror-merge.svg)](https://www.npmjs.com/package/react-codemirror-merge) | ||
|
||
CodeMirror merge view for React. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install react-codemirror-merge --save | ||
``` | ||
|
||
## Usage | ||
|
||
```jsx | ||
import CodeMirrorMerge from 'react-codemirror-merge'; | ||
import { EditorView } from 'codemirror'; | ||
import { EditorState } from '@codemirror/state'; | ||
|
||
const Original = CodeMirrorMerge.Original; | ||
const Modified = CodeMirrorMerge.Modified; | ||
let doc = `one | ||
two | ||
three | ||
four | ||
five`; | ||
|
||
export const Example = () => { | ||
return ( | ||
<CodeMirrorMerge> | ||
<Original value={doc} /> | ||
<Modified | ||
value={doc.replace(/t/g, 'T') + 'Six'} | ||
extensions={[EditorView.editable.of(false), EditorState.readOnly.of(true)]} | ||
/> | ||
</CodeMirrorMerge> | ||
); | ||
}; | ||
``` | ||
|
||
## Contributors | ||
|
||
As always, thanks to our amazing contributors! | ||
|
||
<a href="https://github.com/uiwjs/react-codemirror/graphs/contributors"> | ||
<img src="https://uiwjs.github.io/react-codemirror/CONTRIBUTORS.svg" /> | ||
</a> | ||
|
||
Made with [github-action-contributors](https://github.com/jaywcjlove/github-action-contributors). | ||
|
||
## License | ||
|
||
Licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "react-codemirror-merge", | ||
"version": "0.0.1", | ||
"description": "CodeMirror merge view for React.", | ||
"homepage": "https://uiwjs.github.io/react-codemirror", | ||
"author": "kenny wong <[email protected]>", | ||
"license": "MIT", | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
"scripts": { | ||
"watch": "tsbb watch src/*.tsx --use-babel", | ||
"build": "tsbb build src/*.tsx --use-babel", | ||
"test": "tsbb test --env=jsdom", | ||
"coverage": "tsbb test --env=jsdom --coverage --bail" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/uiwjs/react-codemirror.git" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src", | ||
"esm", | ||
"cjs" | ||
], | ||
"peerDependencies": { | ||
"@babel/runtime": ">=7.11.0", | ||
"@codemirror/state": ">=6.0.0", | ||
"@codemirror/theme-one-dark": ">=6.0.0", | ||
"@codemirror/view": ">=6.0.0", | ||
"codemirror": ">=6.0.0", | ||
"react": ">=16.8.0", | ||
"react-dom": ">=16.8.0" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.18.6", | ||
"@uiw/react-codemirror": "^4.19.11", | ||
"@codemirror/merge": "^6.0.1" | ||
}, | ||
"keywords": [ | ||
"react", | ||
"codemirror", | ||
"codemirror6", | ||
"react-codemirror", | ||
"editor", | ||
"syntax", | ||
"ide", | ||
"code" | ||
], | ||
"jest": { | ||
"coverageReporters": [ | ||
"lcov", | ||
"json-summary" | ||
] | ||
} | ||
} |
Oops, something went wrong.