0

I'm using Angular 5 and typescript and included the code editor codemirror into my page.

I want to overwrite a nested functionality: Codemirror.hint.function

In javascript it looks like:

Codemirror.hint.function = function(param) = {
...
}

How can I do this in typescript? Because it is nested, I can't define a simple interface.

I want to define an own autocompletion for this editor.

solved it: Codemirror specific solution: Code: stackblitz

arahfahrrad
  • 145
  • 1
  • 1
  • 8

1 Answers1

1

You can use this

Object.defineProperty(Codemirror.hint, 'function', {value: () => {/* your override */}});

Although I highly doubt that an object can have a function function, since it is a javascript keyword.

EDIT I don't know why or how, but the typescript definition in your blitz didn't declare CodeMirror as a global variable. Probably minification, but I won't search further.

So I added the script with a CDN in index.html, and now it works.

Here is the stackblitz.

  • Cannot find Codemirror. Should I define an interface ? – arahfahrrad Mar 16 '18 at 13:44
  • How do you access it in your Typescript component ? –  Mar 16 '18 at 13:45
  • At the moment i have no possibility to access it. Thats why I'm stuck. – arahfahrrad Mar 16 '18 at 13:49
  • `declare var Codemirror: any;` ? –  Mar 16 '18 at 13:52
  • Throws an error, that codemirror is not defined, if i use it with the functino defineProperty. – arahfahrrad Mar 16 '18 at 13:59
  • And if you add `declare var Codemirror: any;`, just like I said ? –  Mar 16 '18 at 14:00
  • I declared it like that. There is an example how I want to do it: https://stackoverflow.com/questions/19244449/codemirror-autocomplete-custom-list Does it matter where I declare the var? I declared it in the component I want to use it. – arahfahrrad Mar 16 '18 at 14:11
  • 1
    Please post your code, I'm getting really tired of people saying they do something, while they don't, and then complain about the solution not working because they didn't do it just because they didn't like the answer. Or even better, create a stackblitz with your issue. –  Mar 16 '18 at 14:13
  • Maybe you should have stated that you used typescript definitions, and not a Javascript library ... I don't know Codemirror, what does Codemirror represents ? Is it a global variable ? –  Mar 16 '18 at 14:45
  • Codemirror is the code editor, that is shown on the side. The namespace Codemirror references to the functionality of the javascript implementation of Codemirror. The functionality for a autocompletion of the library is stored in the namespace CodeMirror.hint.functionname. – arahfahrrad Mar 16 '18 at 14:52
  • Nevermind, I edited my answer, feel free to look at the blitz (sorry about your code in this blitz, but at every change the code mirror was being focused, I lost my mind so I deleted your code to avoid that) –  Mar 16 '18 at 14:57
  • If the error still pops, make a small modification to let stackblitz import the library from the CDN correctly. –  Mar 16 '18 at 14:58