Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"homepage": "/",
"private": true,
"dependencies": {
"@babel/parser": "^7.23.0",
"@babel/traverse": "^7.23.0",
"@braintree/sanitize-url": "^6.0.2",
"@monaco-editor/react": "^4.4.6",
"@near-wallet-selector/core": "^8.5.0",
Expand All @@ -25,8 +27,10 @@
"bootstrap": "^5.3.1",
"bootstrap-icons": "^1.9.0",
"collections": "^5.1.12",
"emmet-monaco-es": "^5.3.0",
"error-polyfill": "^0.1.2",
"local-storage": "^2.0.0",
"monaco-jsx-highlighter": "^2.0.4",
"near-api-js": "^2.1.3",
"near-social-vm": "git+https://github.com/NearSocial/VM.git#2.4.2",
"near-social-vm-types": "^1.0.0",
Expand Down
74 changes: 74 additions & 0 deletions src/pages/EditorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "../components/Editor/FileTab";
import { useHashRouterLegacy } from "../hooks/useHashRouterLegacy";
import vmTypesDeclaration from "raw-loader!near-social-vm-types";
import { emmetJSX } from "emmet-monaco-es";

const LsKey = "social.near:v01:";
const EditorLayoutKey = LsKey + "editorLayout:";
Expand All @@ -43,6 +44,72 @@ const Layout = {
Split: "Split",
};

const activateMonacoJSXHighlighter = async (monacoEditor, monaco) => {
// monaco-jsx-highlighter depends on these in addition to Monaco and an instance of a Monaco Editor.
const { default: traverse } = await import("@babel/traverse");
const { parse } = await import("@babel/parser");

if(monaco.KeyCode.US_SLASH === undefined) {
monaco.KeyCode.US_SLASH = monaco.KeyCode.Slash ?? 85;
}
// >>> The star of the show =P >>>
const { default: MonacoJSXHighlighter, JSXTypes } = await import(
"monaco-jsx-highlighter" // Note: there is a polyfilled version alongside the regular version.
); // For example, starting with 2.0.2, 2.0.2-polyfilled is also available.

// Instantiate the highlighter
const monacoJSXHighlighter = new MonacoJSXHighlighter(
monaco, // references Range and other APIs
parse, // obtains an AST, internally passes to parse options: {...options, sourceType: "module",plugins: ["jsx"],errorRecovery: true}
traverse, // helps collecting the JSX expressions within the AST
monacoEditor // highlights the content of that editor via decorations
);

// Start the JSX highlighting and get the dispose function
let disposeJSXHighlighting = monacoJSXHighlighter.highlightOnDidChangeModelContent();
// Enhance monaco's editor.action.commentLine with JSX commenting and get its disposer
let disposeJSXCommenting = monacoJSXHighlighter.addJSXCommentCommand();
// <<< You are all set. >>>

// Optional: customize the color font in JSX texts (style class JSXElement.JSXText.tastyPizza from ./index.css)
JSXTypes.JSXText.options.inlineClassName = "JSXElement.JSXText.tastyPizza";
// more details here: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IModelDecorationOptions.html

// This example's shorthands for toggling actions
const toggleJSXHighlighting = () => {
if (disposeJSXHighlighting) {
disposeJSXHighlighting();
disposeJSXHighlighting = null;
return false;
}

disposeJSXHighlighting = monacoJSXHighlighter.highlightOnDidChangeModelContent();
return true;
};

const toggleJSXCommenting = () => {
if (disposeJSXCommenting) {
disposeJSXCommenting();
disposeJSXCommenting = null;
return false;
}

disposeJSXCommenting = monacoJSXHighlighter.addJSXCommentCommand();
return true;
};

const isToggleJSXHighlightingOn = () => !!disposeJSXHighlighting;
const isToggleJSXCommentingOn = () => !!disposeJSXCommenting;

return {
monacoJSXHighlighter,
toggleJSXHighlighting,
toggleJSXCommenting,
isToggleJSXHighlightingOn,
isToggleJSXCommentingOn
};
}

export default function EditorPage(props) {
useHashRouterLegacy();
const { widgetSrc } = useParams();
Expand Down Expand Up @@ -425,6 +492,12 @@ export default function EditorPage(props) {
setPreviewKey(`preview-${Date.now()}`);
};

const handleMount = useCallback((monacoEditor, monaco) => {
activateMonacoJSXHighlighter(monacoEditor, monaco);
emmetJSX(monaco);
}, []);


return (
<div className="container-fluid mt-1">
<RenameModal
Expand Down Expand Up @@ -623,6 +696,7 @@ export default function EditorPage(props) {
wrapperProps={{
onBlur: () => reformat(path, code),
}}
onMount={handleMount}
/>
</div>
<div className="mb-3 d-flex gap-2 flex-wrap">
Expand Down
Loading