-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53b3c1f
commit 3b82970
Showing
9 changed files
with
1,189 additions
and
50 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,56 +1,120 @@ | ||
import {useState} from "react"; | ||
import {Controlled as CodeMirror} from "react-codemirror2"; | ||
import "codemirror/lib/codemirror.css"; | ||
import "codemirror/mode/javascript/javascript"; | ||
import "codemirror/addon/hint/show-hint.css"; | ||
import "codemirror/addon/hint/show-hint"; | ||
|
||
const autocomplete = (editor) => { | ||
const cursor = editor.getCursor(); | ||
const token = editor.getTokenAt(cursor); | ||
const start = token.start; | ||
const end = token.end; | ||
const currentWord = token.string; | ||
|
||
// Replace this array with your language's keywords | ||
const keywords = ["صفحه", "جعبه", "رنگ", "تمام"]; | ||
|
||
const suggestions = keywords | ||
.filter((kw) => kw.startsWith(currentWord)) | ||
.map((kw) => ({text: kw, displayText: kw})); | ||
|
||
return { | ||
list: suggestions, | ||
from: CodeMirror.Pos(cursor.line, start), | ||
to: CodeMirror.Pos(cursor.line, end), | ||
}; | ||
}; | ||
import {useEffect} from "react"; | ||
import "../../scripts/codemirror.js" | ||
|
||
const Editor = () => { | ||
const [code, setCode] = useState("صفحه:\n جعبه:\n رنگ=\"ابی\"\n تمام\nتمام"); | ||
useEffect(() => { | ||
const CM = window?.CM; | ||
if (!CM) { | ||
console.error("Codemirror is not loaded"); | ||
return; | ||
} | ||
|
||
const { EditorView, basicSetup } = CM["codemirror"]; | ||
const { completeFromList } = CM["@codemirror/autocomplete"]; | ||
const { LRParser } = CM["@lezer/lr"]; | ||
const { LRLanguage } = CM["@codemirror/language"]; | ||
const { LanguageSupport } = CM["@codemirror/language"]; | ||
const { foldNodeProp, foldInside, indentNodeProp } = CM["@codemirror/language"]; | ||
const { styleTags, tags } = CM["@lezer/highlight"]; | ||
|
||
const parser = LRParser.deserialize({ | ||
version: 14, | ||
states: "#SQYQPOOO#wQQO'#C_OOQO'#Ce'#CeQYQPOOOOQO'#Ca'#CaO$OQQO'#CfO%`QQO'#C`O%gQPO,58yOOQO-E6c-E6cO%lQPO'#CgO%tQQO,59QOOQO-E6d-E6dOOQO1G.e1G.eOOQO'#Cb'#CbOOQO,59R,59ROOQO-E6e-E6e", | ||
stateData: "'U~O^OSPOS~O_POtPOuPOvPOwPOxPOyPOzPO{PO|PO}PO!OPO!PPO!QPO!RPO!SPO!TPO!UPO!VPO!WPO!XPO~O`SOaSObSOcSOdSOeSOfSOgSOhSOiSOjSOkSOlSOmSOnSOoSOpSOqSO~OsSP~P!mOrXO`YXaYXbYXcYXdYXeYXfYXgYXhYXiYXjYXkYXlYXmYXnYXoYXpYXqYXsYX~OsSX~P!mOs[O~OV]OW]O~OrXO`YaaYabYacYadYaeYafYagYahYaiYajYakYalYamYanYaoYapYaqYasYa~O", | ||
goto: "}[PPP]adhPPkqwTQORRVPTTPUR^XQRORWRQUPRZUQYTR_Y", | ||
nodeNames: "⚠ LineComment Program Command Body Property Value String Identifier", | ||
maxTerm: 55, | ||
skippedNodes: [0, 1], | ||
repeatNodeCount: 3, | ||
tokenData: "Bb~RqXY#YYZ#Y]^#Ypq#Y}!O#k!Q![#k![!]$P!]!^$U!c!}#k#R#S#k#T#o#k$q$r$mCZC[&yC[C](^C^C_)yC`Ca+hCbCc,sCcCd-pCeCf.XCfCg0YCgCh0qCiCj1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PC|C}=g~=jPCcCd=m~=pPFvFw=s~=xO|~~={PCeCf>O~>RPCZC[>U~>XPFvFw>[~>_PChCi>b~>ePFXFY>h~>kPCeCf>n~>qPpq>t~>wPCyCz>z~>}PC^C_?Q~?TPCzC{?W~?]O}~~?`PCZC[?c~?fPCeCf?i~?lPCZC[?o~?rPFXFY?u~?xPCeCf?{~@OPCZC[@R~@UPCuCv@X~@^Ot~~@aPCxCy@d~@gPCyCz@j~@mPCZC[@p~@sPC^C_@v~@{Of~~AOPCeCfAR~AUQCcCdA[C|C}Ag~A_PFvFwAb~AgOo~~AjPC{C|Am~ApP$I`$IaAs~AvPC[C]Ay~A|PCzC{BP~BSPCcCdBV~BYPFvFwB]~BbOw~", | ||
tokenizers: [0, 1], | ||
topRules: { "Program": [0, 2] }, | ||
tokenPrec: 0 | ||
}); | ||
|
||
const SALAMLanguage = LRLanguage.define({ | ||
parser: parser.configure({ | ||
props: [ | ||
indentNodeProp.add({ | ||
Application: context => context.column(context.node.from) + context.unit | ||
// Application: delimitedIndent({ closing: ")", align: false }) | ||
}), | ||
foldNodeProp.add({ | ||
Application: foldInside | ||
}), | ||
styleTags({ | ||
Identifier: tags.variableName, | ||
Boolean: tags.bool, | ||
String: tags.string, | ||
LineComment: tags.lineComment, | ||
"( )": tags.paren | ||
}) | ||
] | ||
}), | ||
languageData: { | ||
// commentTokens: { line: ";" } | ||
} | ||
}); | ||
|
||
const exampleCompletion = SALAMLanguage.data.of({ | ||
autocomplete: completeFromList([ | ||
{ label: "صفحه", type: "keyword" }, | ||
{ label: "تمام", type: "keyword" }, | ||
{ label: "پاراگراف", type: "keyword" }, | ||
{ label: "ضخیم", type: "keyword" }, | ||
{ label: "قطعه", type: "keyword" }, | ||
{ label: "گروهبندی", type: "keyword" }, | ||
{ label: "عنوان گروه", type: "keyword" }, | ||
{ label: "تصویر", type: "keyword" }, | ||
{ label: "پیوند", type: "keyword" }, | ||
{ label: "خط", type: "keyword" }, | ||
{ label: "خط بعدی", type: "keyword" }, | ||
{ label: "دکمه", type: "keyword" }, | ||
{ label: "ورودی", type: "keyword" }, | ||
{ label: "ویرایشگر متن", type: "keyword" }, | ||
{ label: "برچسب", type: "keyword" }, | ||
{ label: "جعبه", type: "keyword" }, | ||
{ label: "فهرست غیر مرتب", type: "keyword" }, | ||
{ label: "فهرست مرتب", type: "keyword" }, | ||
{ label: "مورد", type: "keyword" }, | ||
{ label: "جدول", type: "keyword" }, | ||
{ label: "ردیف", type: "keyword" }, | ||
{ label: "ستون", type: "keyword" } | ||
]) | ||
}); | ||
|
||
function SALAM() { | ||
return new LanguageSupport(SALAMLanguage, [exampleCompletion]) | ||
} | ||
|
||
const elm_editor = document.querySelector("#editor"); | ||
const editor_options = { | ||
parent: elm_editor, | ||
styleActiveLine: true, | ||
lineNumbers: true, | ||
matchBrackets: true, | ||
autoCloseBrackets: true, | ||
autoCloseTags: true, | ||
extensions: [ | ||
basicSetup, | ||
SALAM(), | ||
EditorView.updateListener.of((update) => { | ||
if (update.changes) { | ||
const newText = update.state.doc.toString(); | ||
console.log("Editor content changed:", newText); | ||
} | ||
}), | ||
], | ||
}; | ||
const editor = new EditorView(editor_options); | ||
}, []) | ||
|
||
return (<> | ||
<main | ||
className={"w-[calc(100%-50px)] h-[calc(100vh-35px)] bg-[#FFF1E9] rounded-tr-[15px] rtl after:inline-block after:border-0 after:z-[-1] after:w-[30px] after:h-[30px] float-end after:bg-[#ffdecc] after:absolute relative after:top-0 after:right-0"}> | ||
<div className={"rounded-tr-[15px] overflow-hidden"}> | ||
<CodeMirror | ||
value={code} | ||
options={{ | ||
mode: "javascript", // Replace with your custom mode | ||
lineNumbers: false, | ||
extraKeys: { | ||
"Ctrl-Space": (cm) => cm.showHint({hint: autocomplete}), | ||
}, | ||
}} | ||
onBeforeChange={(editor, data, value) => { | ||
setCode(value); | ||
}} | ||
/> | ||
</div> | ||
className={"editor-container w-[calc(100%-50px)] h-[calc(100vh-35px)] bg-[#FFF1E9] rounded-tr-[15px] rtl after:inline-block after:border-0 after:z-[-1] after:w-[30px] after:h-[30px] float-end after:bg-[#ffdecc] after:absolute relative after:top-0 after:right-0"}> | ||
<div id="editor"></div> | ||
</main> | ||
</>); | ||
}; | ||
|
||
export default Editor; | ||
|
||
// <pre>{JSON.stringify(handleTreeOutput(code), null, 2)}</pre> |
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
Oops, something went wrong.