Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
SwiftUI wrapper for CodeMirror 6.
License
jaywcjlove/swiftui-codemirror
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
SwiftUI wrapper for CodeMirror 6.
- Minimal and fast
- Handles large documents with ease
- 40+ themes available
- macOS & iOS support
- Built with SwiftUI
Add CodeMirror to your project using Xcode:
- In Xcode, go to
File→Add Package Dependencies... - Enter the repository URL:
https://github.com/jaywcjlove/swiftui-codemirror.git - Click
Add Package
Or add it to yourPackage.swift file:
dependencies:[.package(url:"https://github.com/jaywcjlove/swiftui-codemirror.git", from:"1.0.0")]
import SwiftUIimport CodeMirrorstructContentView:View{@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value, prompt:String(localized:"Please enter text"))CodeMirror(value: $value).onLoadSuccess(){print("Hello!")}.onLoadFailed{ errorinprint("@@@2\(#function)\(error)")}.onContentChange{ valueinprint("@@@3 Content Did Change")}}}
Set Line Wrapping
structContentView:View{@StatevarlineWrapping=false@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmLineWrapping($lineWrapping)}}
Show Line Numbers
structContentView:View{@StatevarlineNumber=true@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmLineNumber($lineNumber)}}
Show Fold Gutter
structContentView:View{@StatevarfoldGutter=false@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmFoldGutter($foldGutter)}}
Set Editor Read-Only
structContentView:View{@StatevarreadOnly=false@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmReadOnly($readOnly)}}
Set enabled search
structContentView:View{@StatevarenabledSearch=false@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmEnabledSearch(.constant(false))}}
Set Font Size
CodeMirror(value: $value).cmFontSize(.constant(14))
Set Highlight Active Line
CodeMirror(value: $value).cmHighlightActiveLine(.constant(false))
Set Programming Language
structContentView:View{@Statevarlanguage:Language=.json@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmLanguage($language)}}
Support:C,C++,CQL,CSS,Go,HTML,Java,JavaScript,JSON,JSX,LESS,Liquid,MariaDB SQL,Markdown,MS SQL,MySQL,PHP,PLSQL,PostgreSQL,Python,Rust,Sass,SCSS,SQL,SQLite,TSX,TypeScript,WebAssembly,XML,YAML,APL,PGP,ASN.1,Asterisk,Brainfuck,Cobol,C#,Clojure,ClojureScript,Closure Stylesheets (GSS),CMake,CoffeeScript,Common Lisp,Cypher,Cython,Crystal,D,Dart,diff,Dockerfile,DTD,Dylan,EBNF,ECL,edn,Eiffel,Elm,Erlang,Esper,Factor,FCL,Forth,Fortran,F#,Gas,Gherkin,Groovy,Haskell,Haxe,HXML,HTTP,IDL,JSON-LD,Jinja2,Julia,Kotlin,LiveScript,Lua,mIRC,Mathematica,Modelica,MUMPS,Mbox,Nginx,NSIS,NTriples,Objective-C,Objective-C++,OCaml,Octave,Oz,Pascal,Perl,Pig,PowerShell,Properties files,ProtoBuf,Pug,Puppet,Q,R,RPM Changes,RPM Spec,Ruby,SAS,Scala,Scheme,Shell,Sieve,Smalltalk,Solr,SML,SPARQL,Spreadsheet,Squirrel,Stylus,Swift,sTeX,LaTeX,SystemVerilog,Tcl,Textile,TiddlyWiki,Tiki wiki,TOML,Troff,TTCN,TTCN_CFG,Turtle,Web IDL,VB.NET,VBScript,Velocity,Verilog,VHDL,XQuery,Yacas,Z80,MscGen,Xù,MsGenny,Vue,Angular Template,
Set Theme
structContentView:View{@Environment(\.colorScheme)varcolorScheme@Statevartheme:Themes=.vscodelight@Statevarvalue:String=""varbody:someView{CodeMirror(value: $value).cmTheme($theme).cmTheme( colorScheme==.dark?.constant(.vscodedark):.constant(.vscodelight))}}
import SwiftUIimport CodeMirrorstructContentView:View{@ObservedObjectvarvm:CodeMirrorVM=.init()@Statevarvalue:String=""varbody:someView{CodeMirrorView(vm, value: $value).onAppear{ vm.setContent(jsonString)}}}
Set Theme
import SwiftUIimport CodeMirrorstructContentView:View{@ObservedObjectvarvm:CodeMirrorVM=.init()@Statevarvalue:String=""varbody:someView{VStack{CodeMirrorView(vm, value: $value).onAppear{ vm.setContent(jsonString)}Picker("Theme", selection: $vm.theme){ForEach(Themes.allCases, id: \.rawValue){Text($0.rawValue).tag($0)}}}}}
Set Programming Language
Picker("Lang", selection: $vm.language){ForEach(Language.allCases, id: \.rawValue){Text($0.rawValue).tag($0)}}
vm.language=.json
Set Editor Content
Button{ vm.setContent("Hello World!")} label:{Text("SET")}
Get Editor Text Content
Button{Task{letcontent=try?await vm.getContent()print(content??"")}} label:{Text("GET")}
Set Editor Read-Only
Toggle(isOn: $vm.readOnly, label:{Text("Read Only")}).toggleStyle(.checkbox)
Show Line Numbers
ToolbarItem{Toggle(isOn: $vm.lineNumber, label:{Text("Line Number")}).toggleStyle(.checkbox)}
Set Line Wrapping
ToolbarItem{Toggle(isOn: $vm.lineWrapping, label:{Text("Line Wrapping")}).toggleStyle(.checkbox)}
Event
@ObservedObjectvarvm:CodeMirrorVM=.init( onLoadSuccess:{print("@@@1\(#function)")}, onLoadFailed:{ errorinprint("@@@2\(#function)\(error)")}, onContentChange:{ valueinprint("@@@3 Content Did Change")})
Thanks to these projects:
- https://codemirror.net
- https://github.com/khoi/codemirror-swift
- https://github.com/ProxymanApp/CodeMirror-Swift
Licensed under the MIT License.
About
SwiftUI wrapper for CodeMirror 6.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
