Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer a <code-mirror> custom element #15

Closed
justinfagnani opened this issue Aug 28, 2018 · 6 comments
Closed

Offer a <code-mirror> custom element #15

justinfagnani opened this issue Aug 28, 2018 · 6 comments

Comments

@justinfagnani
Copy link

Embedding CodeMirror will be a bit easier with:

<code-mirror></code-mirror>

than:

<div style="" id=editor></div>
let view = (window as any).view = new EditorView(state)
document.querySelector("#editor").appendChild(view.dom)

Custom elements are natively available in Chrome, Safari, Opera and Firefox Nightly, and polyfilled in Edge and older browsers.

@marijnh
Copy link
Member

marijnh commented Aug 29, 2018

Doing this in a way that only supports a very dumbed-down interface would be easy, but that'd be annoying in that I'd expect people to run into its limitations very quickly, at which point they have to refactor to the non-custom-element interface.

That being said, I don't have a lot of experience with custom elements, and maybe there are ways to model a projection of most of the API this way. How do you pass non-string data in and out of such elements? Element attributes are still constrained to be strings, right?

In any case, this won't be part of the core library, but it'd make perfect sense to provide it as a separate wrapper package.

@jarek-foksa
Copy link

@marijnh Custom element would allow you to mimic the API of native widgets such as <textarea> or <input>. This means you could expose functionality as:

  • JS method, e.g. editor.showLineNumbers();
  • JS getter/setter, e.g. editor.lineNumbers = true;
  • HTML attribute, e.g. editor.setAttribute("linenumbers", "");

While the HTML attribute value must be a string, you could also provide a corresponding getter/setter which reflects the attribute value as a number, boolean, array, etc. Check Xel for example, I think Polymer is also using this pattern extensively.

@justinfagnani
Copy link
Author

@marijnh Big +1 to what @jarek-foksa said.

Elements are objects and have regular JS object semantics, so they can have methods and properties like any other object. They are not limited at all to attributes and strings. For instance, look at the HTMLMediaElement interface which has an extensive set of properties and methods: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

HTML markup, of course, is limited to children, attributes and strings, so you won't be able to do very complex configuration just from markup, but you'll be able to set the simple-valued flags. Most templating languages these days, like Vue, Angular, Polymer, lit-html, support setting properties declaratively, so in those cases one could do complex config declaratively. Then as a fallback you have imperative configuration.

You could also use subclassing:

class MyCodeEditor extends CodeMirrorElement { ... }
customElements.define('my-code-editor', MyCodeEditor);
<!-- already configured -->
<my-code-editor></my-code-editor>

In any case, this won't be part of the core library, but it'd make perfect sense to provide it as a separate wrapper package.

Seeing as how Web Components are the Web platforms native component model and CodeMirror is vending a component-like thing, this seems unfortunate to me. I think many of the simple use cases could be even easier and more obvious to use with custom elements.

@curran
Copy link
Contributor

curran commented Oct 21, 2018

Agreed this would make total sense as a wrapper package, not part of the core.

@marijnh
Copy link
Member

marijnh commented Mar 1, 2019

I've taken another look at custom elements and decided that no, the core library will not be based on them. Again, I do think a 3rd party library wrapping the editor in a custom element would be a useful thing, if someone wants to write it, but I'm going to close this.

@marijnh marijnh closed this as completed Mar 1, 2019
@justinfagnani
Copy link
Author

@marijnh do you have any rationale that you could share? Not to try to convince you otherwise, but just understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants