-
Notifications
You must be signed in to change notification settings - Fork 47
Can not use some addons #145
Comments
accessing the editor property use a viewchild ref to access the component and then its at the public codemirror property https://github.com/TypeCtrl/ngx-codemirror/blob/master/src/lib/codemirror.component.ts#L87 |
Thank you :) |
Another problem: CodeMirror.autoLoadMode(instance, mode) must be called but After reading code of loadmode.js: https://github.com/codemirror/CodeMirror/blob/29e338b8c1f27ade4502839877df7afd49584479/addon/mode/loadmode.js#L58 The function seems to not be declared properly on codeMirror instance |
I think is related to: codemirror/codemirror5#5484 Your import seems to be the same as described: https://github.com/TypeCtrl/ngx-codemirror/blob/d5cafceb528b8b95643bd0fa3a7ff858a5c63582/src/lib/codemirror.component.ts#L23 and nothing is correctly declared to the "fake" instance described. |
its imported down further, those are just @types being imported at the top You might make sure you've included your plugin correctly from the first step of use https://github.com/TypeCtrl/ngx-codemirror#use or make sure the codemirror instance has been created when your code runs |
This seems to be the case for all the addons |
After a lot of debugging, it seems require creates a scoped instance of CodeMirror. My final solution is to mix global instance and this component. "scripts": [
"node_modules/codemirror/lib/codemirror.js",
"node_modules/codemirror/mode/meta.js",
"node_modules/codemirror/addon/mode/loadmode.js"
] If we use fromTextArea from scoped instance, the setOption function will not work. declare var require: any; // remove this
const { fromTextArea } = require('codemirror');
this.codeMirror = fromTextArea(this.ref.nativeElement, this._options); by: declare var CodeMirror: any; // replaced by "var require..."
this.codeMirror = CodeMirror.fromTextArea(this.ref.nativeElement, this._options); In my internal component i use declare var CodeMirror: any to access all functions (ref is the childView of ngx-codemirror component) ngAfterViewInit() {
CodeMirror.modeURL = this.modeUrl
const mode = CodeMirror.findModeByFileName(this.file.name).mode // this function is loaded by meta addon
CodeMirror.autoLoadMode(this.ref.codeMirror, mode) // this function is loaded by loadmode addon
this.ref.codeMirror.setOption('mode', mode)
} |
Importing addons like modes as described in the README works perfectly. Here's my import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
// Import your required language in main.ts or at the root of your application
// see https://codemirror.net/mode/index.html
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/css/css';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/addon/edit/closetag';
import 'codemirror/addon/selection/active-line';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err)); and here's my options object to enable the above addons: editorOptions = {
lineNumbers: true,
autoCloseTags: true,
styleActiveLine: true,
theme: 'monokai',
mode: 'text/html',
// define Emmet output profile
profile: 'xhtml'
}; Btw, thank you a lot @scttcper for this awesome ngx wrapper for codemirror! 🙌 |
This only works for addons that affect the editor (CodeMirror.fromTextArea) |
@johaven I got it! Good to know. 👍 |
@johaven i'm not sure how to fix it for your case. You might try copying out the component and instantiating codemirror as you require, unless there's a way to make it work for everyone's general use. |
@scttcper This is what i do for the time being. declare var CodeMirror: any; // replaced by "var require..."
this.codeMirror = CodeMirror.fromTextArea(this.ref.nativeElement, this._options); If people want to use other addons like me, they will only have to load the libraries via the Angular scripts tag above, for others it will work the same way. |
I am attempting to do something similar except in my case is to dynamically define the mode and load it on the fly based on a matrix of user provided data... @johaven could you provide a StackBlitz showing what you did to get loadmode functional? because I am hitting a brick wall trying to deduce this thread. |
Hello, i also had problems to use the mergeView addon, but finally made it, so here is my sample codemirror-merge-view |
@scttcper this can be closed |
I try to use this addon: https://codemirror.net/demo/loadmode.html
The idea is to put all folder languages in my assets dir, and give formatted url etc ...
How can i access to this variable https://github.com/codemirror/CodeMirror/blob/29e338b8c1f27ade4502839877df7afd49584479/demo/loadmode.html#L65 with ngx-codemirror ?
The text was updated successfully, but these errors were encountered: