gpt4 book ai didi

css - 如何使用 React Codemirror 自动建议 css 关键字?

转载 作者:行者123 更新时间:2023-12-03 09:32:43 25 4
gpt4 key购买 nike

我正在尝试使用 react wrapper lib https://www.npmjs.com/package/react-codemirror2 使用代码镜像浏览器编辑器实现 css 的自动建议
我试过 editor.execCommand('autocomplete'); onchange 事件,但它使浏览器崩溃
我的尝试

import ReactDOM from "react-dom";
import React from "react";
import {UnControlled as CodeMirror} from 'react-codemirror2';

import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/material.css';
import 'codemirror/addon/hint/show-hint.css';

require('codemirror/mode/css/css');
require('codemirror/addon/hint/css-hint');
require('codemirror/addon/hint/show-hint');
require('codemirror/addon/edit/closebrackets');
require('codemirror/addon/lint/lint');
require('codemirror/addon/display/autorefresh');


const App = () => {

const handleChange = (editor, data, value) => {

console.log(editor, data, value);

/* Crash the browser */
// editor.execCommand('autocomplete');
}
return(
<div>
<CodeMirror
value='body{ background: red }'
options={{
mode: 'css',
theme: 'material',
lineWrapping: true,
smartIndent: true,
lineNumbers: true,
foldGutter: true,
autoCloseTags: true,
matchBrackets: true,
autoCloseBrackets: true,
autoRefresh:true
}}
onChange={handleChange}
/>
</div>
)
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
提前致谢

最佳答案

您可以使用 editor.showHint({ completeSingle: false })而不是 editor.execCommand('autocomplete');

const handleChange = (editor, data, value) => {
editor.showHint({ completeSingle: false });
};
Edit flamboyant-fast-xsdqo

您还可以使用此 Github issue 中所述的特定组合键来触发它。
<CodeMirror
options={{
extraKeys: {'Ctrl-Space': 'autocomplete'}, // pressing CTRL + Space will trigger autocomplete
}}
/>

关于css - 如何使用 React Codemirror 自动建议 css 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63873307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com