在 Ace 编辑器中添加主题(在 React 中使用 ace-builds)

huangapple go评论69阅读模式
英文:

Add Theme in Ace Editor (in react using ace-builds)

问题

I cannot directly access external URLs or view images, so I won't be able to see the browser error logs you've linked. However, I can help you with the code you provided and any specific questions you have regarding it. If you have any code-related questions or need assistance with a particular issue, please let me know, and I'll do my best to assist you.

英文:

I'm trying to use AceEditor (preferring to use ace-builds npm package than react-ace npm package) in a react project.

I succeed to create a basic text editor but cannot figure out how to change the themes or the languages workers, I have some problem to load the js files from the node_modules certainly a webpack problem but I don't really understand what's happening so if someone has some clue.

I'm using "ace-builds": "^1.15.2"

my code is

import React    from "react";

import * as Ace from "ace-builds";

import "ace-builds/src-noconflict/theme-monokai";

type T_Props = { };

const OnlineIDE:React.FunctionComponent<T_Props> = (props:T_Props) =>
{
	const editorElement = React.useRef<HTMLDivElement>(null);
	const editorInstance:React.MutableRefObject<Ace.Ace.Editor|null> = React.useRef(null);

	React.useEffect(():void =>
	{
		if (editorElement.current != null)
			editorInstance.current = Ace.edit(editorElement.current);

		if (editorInstance.current != null)
			editorInstance.current.setTheme("theme_monokai");
	}, []);

	return (
		<div
			style=
			{{
				width: "50vw",
				height: "50vh",
				border: "black solid 2px",
				display: "flex",
				flexDirection: "column",
			}}
		>
			<div
				style=
				{{
					background: "rgb(235, 235, 235)",
					color: "white",
					display: "flex",
					justifyContent: "end",
					padding: "10px",
				}}
			>
			</div>
			<div style={{ width: "100%", height: "inherit" }} ref={editorElement}></div>
		</div>
	);
};

export default OnlineIDE;

and the errors I get in the browser:

browser error logs

答案1

得分: 0

在ace-builds中的路径名称相当混乱,使用.setTheme("ace/theme/monokai");应该可以工作。

英文:

Path names in ace-builds are rather confusing, using .setTheme("ace/theme/monokai"); should work.

huangapple
  • 本文由 发表于 2023年2月27日 03:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定