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

Exposing simpleMDEditor as Javascript object (again) #10409

Closed
2 of 8 tasks
marcellmars opened this issue Feb 21, 2020 · 4 comments · Fixed by #11739
Closed
2 of 8 tasks

Exposing simpleMDEditor as Javascript object (again) #10409

marcellmars opened this issue Feb 21, 2020 · 4 comments · Fixed by #11739
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@marcellmars
Copy link
Contributor

marcellmars commented Feb 21, 2020

  • Gitea version (or commit ref): v1.12.0-dev
  • Git version: 2.24.1
  • Operating system: GNU Linux, Debian
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
    • Not relevant
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant

Description

I think exposing simpleMDEditor as Javascript object from web_src/js/index.js could be very useful for customizing Gitea.

I added this function to web_src/js/index.js:

window.getSimpleMDEditor = function () {
  return simpleMDEditor;
};

and that allows me to play with CodeMirror. Fox example:

script = document.createElement('script');
script.src = "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.10.0/addon/hint/show-hint.min.js"
document.body.appendChild(script)
link = document.createElement('link')
link.rel = "stylesheet"
link.href = "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.10.0/addon/hint/show-hint.min.css"
document.body.appendChild(link)
editor = getSimpleMDEditor().codemirror
editor.setOption("extraKeys", {
'Alt-U': function () {
    var options = {
    hint: function() {
      return {
        from: editor.getDoc().getCursor(),
          to: editor.getDoc().getCursor(),
        list: ['foo', 'bar']
        }
      }
    };
  editor.showHint(options)}
});

allowed me to bring popup list after one would use [Alt-u] keyboard shortcut.
I am working on a simple UI in which one could use Gitea to maintain Hugo web site and so far it worked very good but for less tech-savvy people in our team I wanted to bring autocomplete for internal links of the Hugo website. This is the way which allows me to do that.

I played with adding these kind of things via custom templates and it worked great.

Screenshots

getSimpleMDEditor

@silverwind
Copy link
Member

Plan is to switch to easymde (#9973), I guess it can be exposed via window.easymde = [...editors] then. Array because I think they can appear multiple times on the same page.

@lunny lunny added the type/proposal The new feature has not been accepted yet but needs to be discussed first. label Feb 23, 2020
@sdockray
Copy link

sdockray commented Jun 2, 2020

Returning to this now that monaco is being used:

it would be great if the editor that is created here, and which is returned from createCodeEditor() could be exposed (for example, to Javascript in custom templates).

But it's just thrown away here:

await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);

Most of the customizations described in the Monaco Editor Playground are not possible without the editor

@silverwind
Copy link
Member

silverwind commented Jun 2, 2020

I'll look into that. Will probably be an array window.editors.

SimpleMDE will eventually go away (probably replaced by a textarea), see #10729.

@silverwind
Copy link
Member

silverwind commented Jun 2, 2020

#11739 will fix this. The only tricky part is because the editor is lazy-loaded, you'll need to wait until it is done loading, for example polling for it:

function waitForEditor() {
  return new Promise(resolve => {
    const interval = setInterval(() => {
      if (window.codeEditors && window.codeEditors.length) {
        clearInterval(interval);
        resolve(window.codeEditors);
      }
    }, 500);
  });
}

for (const editor of await waitForEditor()) {
  console.log(editor);
}

lafriks added a commit that referenced this issue Jun 3, 2020
Fixes: #10409

Co-authored-by: Lauris BH <lauris@nix.lv>
ydelafollye pushed a commit to ydelafollye/gitea that referenced this issue Jul 31, 2020
Fixes: go-gitea#10409

Co-authored-by: Lauris BH <lauris@nix.lv>
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants