ink-mini-code-editor 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +51 -0
- package/build/index.js +123 -0
- package/build/index.js.map +1 -0
- package/license +9 -0
- package/package.json +90 -0
- package/readme.md +153 -0
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Except } from 'type-fest';
|
|
3
|
+
export type Props = {
|
|
4
|
+
/**
|
|
5
|
+
* Text to display when `value` is empty.
|
|
6
|
+
*/
|
|
7
|
+
readonly placeholder?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Listen to user's input. Useful in case there are multiple input components
|
|
10
|
+
* at the same time and input must be "routed" to a specific component.
|
|
11
|
+
*/
|
|
12
|
+
readonly focus?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Replace all chars and mask the value. Useful for password inputs.
|
|
15
|
+
*/
|
|
16
|
+
readonly mask?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether to show cursor and allow navigation inside text input with arrow keys.
|
|
19
|
+
*/
|
|
20
|
+
readonly showCursor?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Highlight pasted text
|
|
23
|
+
*/
|
|
24
|
+
readonly highlightPastedText?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Value to display in a text input.
|
|
27
|
+
*/
|
|
28
|
+
readonly value: string;
|
|
29
|
+
/**
|
|
30
|
+
* Function to call when value updates.
|
|
31
|
+
*/
|
|
32
|
+
readonly onChange: (value: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Function to call when `Enter` is pressed, where first argument is a value of the input.
|
|
35
|
+
*/
|
|
36
|
+
readonly onSubmit?: (value: string) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Language for syntax highlighting (e.g., 'sql', 'javascript', 'python').
|
|
39
|
+
* When not specified, no syntax highlighting is applied.
|
|
40
|
+
*/
|
|
41
|
+
readonly language?: string;
|
|
42
|
+
};
|
|
43
|
+
declare function TextInput({ value: originalValue, placeholder, focus, mask, highlightPastedText, showCursor, onChange, onSubmit, language, }: Props): React.JSX.Element;
|
|
44
|
+
export default TextInput;
|
|
45
|
+
type UncontrolledProps = {
|
|
46
|
+
/**
|
|
47
|
+
* Initial value.
|
|
48
|
+
*/
|
|
49
|
+
readonly initialValue?: string;
|
|
50
|
+
} & Except<Props, 'value' | 'onChange'>;
|
|
51
|
+
export declare function UncontrolledTextInput({ initialValue, ...props }: UncontrolledProps): React.JSX.Element;
|
package/build/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import { Text, useInput } from 'ink';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import SyntaxHighlight from 'ink-syntax-highlight';
|
|
5
|
+
function TextInput({ value: originalValue, placeholder = '', focus = true, mask, highlightPastedText = false, showCursor = true, onChange, onSubmit, language, }) {
|
|
6
|
+
const [state, setState] = useState({
|
|
7
|
+
cursorOffset: (originalValue || '').length,
|
|
8
|
+
cursorWidth: 0,
|
|
9
|
+
});
|
|
10
|
+
const { cursorOffset, cursorWidth } = state;
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setState(previousState => {
|
|
13
|
+
if (!focus || !showCursor) {
|
|
14
|
+
return previousState;
|
|
15
|
+
}
|
|
16
|
+
const newValue = originalValue || '';
|
|
17
|
+
if (previousState.cursorOffset > newValue.length - 1) {
|
|
18
|
+
return {
|
|
19
|
+
cursorOffset: newValue.length,
|
|
20
|
+
cursorWidth: 0,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return previousState;
|
|
24
|
+
});
|
|
25
|
+
}, [originalValue, focus, showCursor]);
|
|
26
|
+
const cursorActualWidth = highlightPastedText ? cursorWidth : 0;
|
|
27
|
+
const value = mask ? mask.repeat(originalValue.length) : originalValue;
|
|
28
|
+
let renderedValue = value;
|
|
29
|
+
let renderedPlaceholder = placeholder ? chalk.grey(placeholder) : undefined;
|
|
30
|
+
// Fake mouse cursor, because it's too inconvenient to deal with actual cursor and ansi escapes
|
|
31
|
+
if (showCursor && focus) {
|
|
32
|
+
renderedPlaceholder =
|
|
33
|
+
placeholder.length > 0
|
|
34
|
+
? chalk.inverse(placeholder[0]) + chalk.grey(placeholder.slice(1))
|
|
35
|
+
: chalk.inverse(' ');
|
|
36
|
+
renderedValue = value.length > 0 ? '' : chalk.inverse(' ');
|
|
37
|
+
let i = 0;
|
|
38
|
+
for (const char of value) {
|
|
39
|
+
renderedValue +=
|
|
40
|
+
i >= cursorOffset - cursorActualWidth && i <= cursorOffset
|
|
41
|
+
? chalk.inverse(char)
|
|
42
|
+
: char;
|
|
43
|
+
i++;
|
|
44
|
+
}
|
|
45
|
+
if (value.length > 0 && cursorOffset === value.length) {
|
|
46
|
+
renderedValue += chalk.inverse(' ');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
useInput((input, key) => {
|
|
50
|
+
if (key.upArrow ||
|
|
51
|
+
key.downArrow ||
|
|
52
|
+
(key.ctrl && input === 'c') ||
|
|
53
|
+
key.tab ||
|
|
54
|
+
(key.shift && key.tab)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (key.return) {
|
|
58
|
+
if (onSubmit) {
|
|
59
|
+
onSubmit(originalValue);
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
let nextCursorOffset = cursorOffset;
|
|
64
|
+
let nextValue = originalValue;
|
|
65
|
+
let nextCursorWidth = 0;
|
|
66
|
+
if (key.leftArrow) {
|
|
67
|
+
if (showCursor) {
|
|
68
|
+
nextCursorOffset--;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (key.rightArrow) {
|
|
72
|
+
if (showCursor) {
|
|
73
|
+
nextCursorOffset++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (key.backspace || key.delete) {
|
|
77
|
+
if (cursorOffset > 0) {
|
|
78
|
+
nextValue =
|
|
79
|
+
originalValue.slice(0, cursorOffset - 1) +
|
|
80
|
+
originalValue.slice(cursorOffset, originalValue.length);
|
|
81
|
+
nextCursorOffset--;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
nextValue =
|
|
86
|
+
originalValue.slice(0, cursorOffset) +
|
|
87
|
+
input +
|
|
88
|
+
originalValue.slice(cursorOffset, originalValue.length);
|
|
89
|
+
nextCursorOffset += input.length;
|
|
90
|
+
if (input.length > 1) {
|
|
91
|
+
nextCursorWidth = input.length;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (cursorOffset < 0) {
|
|
95
|
+
nextCursorOffset = 0;
|
|
96
|
+
}
|
|
97
|
+
if (cursorOffset > originalValue.length) {
|
|
98
|
+
nextCursorOffset = originalValue.length;
|
|
99
|
+
}
|
|
100
|
+
setState({
|
|
101
|
+
cursorOffset: nextCursorOffset,
|
|
102
|
+
cursorWidth: nextCursorWidth,
|
|
103
|
+
});
|
|
104
|
+
if (nextValue !== originalValue) {
|
|
105
|
+
onChange(nextValue);
|
|
106
|
+
}
|
|
107
|
+
}, { isActive: focus });
|
|
108
|
+
const displayValue = placeholder
|
|
109
|
+
? value.length > 0
|
|
110
|
+
? renderedValue
|
|
111
|
+
: renderedPlaceholder
|
|
112
|
+
: renderedValue;
|
|
113
|
+
if (language) {
|
|
114
|
+
return React.createElement(SyntaxHighlight, { language: language, code: displayValue ?? '' });
|
|
115
|
+
}
|
|
116
|
+
return React.createElement(Text, null, displayValue);
|
|
117
|
+
}
|
|
118
|
+
export default TextInput;
|
|
119
|
+
export function UncontrolledTextInput({ initialValue = '', ...props }) {
|
|
120
|
+
const [value, setValue] = useState(initialValue);
|
|
121
|
+
return React.createElement(TextInput, { ...props, value: value, onChange: setValue });
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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,KAAK,EAAE,QAAQ,EAAE,QAAQ,GAAI,CAAC;AACnE,CAAC"}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Vadym Demedes <vadimdemedes@hey.com> (github.com/vadimdemedes)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ink-mini-code-editor",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Code editor for Ink library",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "choneface/ink-mini-code-editor",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Chone fucking Face",
|
|
9
|
+
"url": "https://github.com/choneface"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
"types": "./build/index.d.ts",
|
|
14
|
+
"default": "./build/index.js"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "tsc --noEmit && xo && FORCE_COLOR=1 ava",
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"prepare": "tsc",
|
|
23
|
+
"pretest": "tsc",
|
|
24
|
+
"dev": "tsx source/index.tsx"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"build"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"ink",
|
|
31
|
+
"text",
|
|
32
|
+
"input",
|
|
33
|
+
"component",
|
|
34
|
+
"jsx",
|
|
35
|
+
"react",
|
|
36
|
+
"stdin",
|
|
37
|
+
"keypress",
|
|
38
|
+
"search",
|
|
39
|
+
"query"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"chalk": "^5.3.0",
|
|
43
|
+
"ink-syntax-highlight": "^2.0.2",
|
|
44
|
+
"type-fest": "^4.18.2"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@sindresorhus/tsconfig": "^5.0.0",
|
|
48
|
+
"tsx": "^4.7.0",
|
|
49
|
+
"@types/node": "^20.12.0",
|
|
50
|
+
"@types/react": "^18.3.2",
|
|
51
|
+
"@types/sinon": "^17.0.3",
|
|
52
|
+
"@vdemedes/prettier-config": "^2.0.1",
|
|
53
|
+
"ava": "^6.1.3",
|
|
54
|
+
"delay": "^6.0.0",
|
|
55
|
+
"eslint-config-xo-react": "^0.27.0",
|
|
56
|
+
"eslint-plugin-react": "^7.34.1",
|
|
57
|
+
"eslint-plugin-react-hooks": "^4.6.2",
|
|
58
|
+
"ink": "^5.0.0",
|
|
59
|
+
"ink-testing-library": "vadimdemedes/ink-testing-library#f44b077e9a05a1d615bab41c72906726d34ea085",
|
|
60
|
+
"prettier": "^3.2.5",
|
|
61
|
+
"react": "^18.3.1",
|
|
62
|
+
"sinon": "^17.0.1",
|
|
63
|
+
"tsimp": "^2.0.11",
|
|
64
|
+
"typescript": "^5.4.5",
|
|
65
|
+
"xo": "^0.58.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"ink": ">=5",
|
|
69
|
+
"react": ">=18"
|
|
70
|
+
},
|
|
71
|
+
"ava": {
|
|
72
|
+
"extensions": {
|
|
73
|
+
"ts": "module",
|
|
74
|
+
"tsx": "module"
|
|
75
|
+
},
|
|
76
|
+
"nodeArguments": [
|
|
77
|
+
"--import=tsimp/import"
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
"xo": {
|
|
81
|
+
"extends": [
|
|
82
|
+
"xo-react"
|
|
83
|
+
],
|
|
84
|
+
"prettier": true,
|
|
85
|
+
"rules": {
|
|
86
|
+
"unicorn/prevent-abbreviations": "off"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"prettier": "@vdemedes/prettier-config"
|
|
90
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# ink-mini-code-editor
|
|
2
|
+
|
|
3
|
+
> Code editor component with syntax highlighting for [Ink](https://github.com/vadimdemedes/ink) CLI applications.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install ink-mini-code-editor
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import React, {useState} from 'react';
|
|
15
|
+
import {render, Box, Text} from 'ink';
|
|
16
|
+
import CodeEditor from 'ink-mini-code-editor';
|
|
17
|
+
|
|
18
|
+
const SQLEditor = () => {
|
|
19
|
+
const [code, setCode] = useState('SELECT * FROM users WHERE id = 1');
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Box flexDirection="column">
|
|
23
|
+
<Text>SQL Query Editor:</Text>
|
|
24
|
+
<Box>
|
|
25
|
+
<Text>{'> '}</Text>
|
|
26
|
+
<CodeEditor
|
|
27
|
+
value={code}
|
|
28
|
+
onChange={setCode}
|
|
29
|
+
placeholder="Enter SQL query..."
|
|
30
|
+
language="sql"
|
|
31
|
+
onSubmit={(val) => {
|
|
32
|
+
console.log('Submitted:', val);
|
|
33
|
+
}}
|
|
34
|
+
/>
|
|
35
|
+
</Box>
|
|
36
|
+
</Box>
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
render(<SQLEditor />);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Props
|
|
44
|
+
|
|
45
|
+
### value
|
|
46
|
+
|
|
47
|
+
Type: `string`
|
|
48
|
+
|
|
49
|
+
Value to display in the code editor.
|
|
50
|
+
|
|
51
|
+
### placeholder
|
|
52
|
+
|
|
53
|
+
Type: `string`
|
|
54
|
+
|
|
55
|
+
Text to display when `value` is empty.
|
|
56
|
+
|
|
57
|
+
### focus
|
|
58
|
+
|
|
59
|
+
Type: `boolean`\
|
|
60
|
+
Default: `true`
|
|
61
|
+
|
|
62
|
+
Listen to user's input. Useful in case there are multiple input components at the same time and input must be "routed" to a specific component.
|
|
63
|
+
|
|
64
|
+
### showCursor
|
|
65
|
+
|
|
66
|
+
Type: `boolean`\
|
|
67
|
+
Default: `true`
|
|
68
|
+
|
|
69
|
+
Whether to show cursor and allow navigation inside the editor with arrow keys.
|
|
70
|
+
|
|
71
|
+
### highlightPastedText
|
|
72
|
+
|
|
73
|
+
Type: `boolean`\
|
|
74
|
+
Default: `false`
|
|
75
|
+
|
|
76
|
+
Highlight pasted text.
|
|
77
|
+
|
|
78
|
+
### mask
|
|
79
|
+
|
|
80
|
+
Type: `string`
|
|
81
|
+
|
|
82
|
+
Replace all chars and mask the value. Useful for password inputs.
|
|
83
|
+
|
|
84
|
+
```jsx
|
|
85
|
+
<CodeEditor value="secret" mask="*" />
|
|
86
|
+
//=> "******"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### onChange
|
|
90
|
+
|
|
91
|
+
Type: `Function`
|
|
92
|
+
|
|
93
|
+
Function to call when value updates.
|
|
94
|
+
|
|
95
|
+
### language
|
|
96
|
+
|
|
97
|
+
Type: `string`
|
|
98
|
+
|
|
99
|
+
Language for syntax highlighting (e.g., `'sql'`, `'javascript'`, `'python'`). When not specified, no syntax highlighting is applied.
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
<CodeEditor value={code} onChange={setCode} language="sql" />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### onSubmit
|
|
106
|
+
|
|
107
|
+
Type: `Function`
|
|
108
|
+
|
|
109
|
+
Function to call when `Enter` is pressed, where first argument is the value of the input.
|
|
110
|
+
|
|
111
|
+
## Uncontrolled usage
|
|
112
|
+
|
|
113
|
+
This component also exposes an [uncontrolled](https://reactjs.org/docs/uncontrolled-components.html) version, which handles `value` changes for you. To receive the final input value, use `onSubmit` prop. Initial value can be specified via `initialValue` prop.
|
|
114
|
+
|
|
115
|
+
```jsx
|
|
116
|
+
import React from 'react';
|
|
117
|
+
import {render, Box, Text} from 'ink';
|
|
118
|
+
import {UncontrolledTextInput} from 'ink-mini-code-editor';
|
|
119
|
+
|
|
120
|
+
const SQLEditor = () => {
|
|
121
|
+
const handleSubmit = (query) => {
|
|
122
|
+
// Execute query
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<Box flexDirection="column">
|
|
127
|
+
<Text>SQL Query Editor:</Text>
|
|
128
|
+
<Box>
|
|
129
|
+
<Text>{'> '}</Text>
|
|
130
|
+
<UncontrolledTextInput
|
|
131
|
+
initialValue="SELECT * FROM"
|
|
132
|
+
placeholder="Enter SQL query..."
|
|
133
|
+
onSubmit={handleSubmit}
|
|
134
|
+
/>
|
|
135
|
+
</Box>
|
|
136
|
+
</Box>
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
render(<SQLEditor />);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
Run the demo:
|
|
146
|
+
|
|
147
|
+
```sh
|
|
148
|
+
npm run dev
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|