{"id":21810674,"url":"https://github.com/samdark/codemirror-buttons","last_synced_at":"2025-07-22T20:05:15.274Z","repository":{"id":58240315,"uuid":"48916843","full_name":"samdark/codemirror-buttons","owner":"samdark","description":"CodeMirror buttons addon","archived":false,"fork":false,"pushed_at":"2019-05-23T15:41:51.000Z","size":10,"stargazers_count":31,"open_issues_count":0,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-14T01:58:00.886Z","etag":null,"topics":["buttons","codemirror","ui"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samdark.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"samdark","patreon":"samdark"}},"created_at":"2016-01-02T16:57:36.000Z","updated_at":"2024-02-09T15:19:24.000Z","dependencies_parsed_at":"2022-08-31T03:01:47.681Z","dependency_job_id":null,"html_url":"https://github.com/samdark/codemirror-buttons","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/samdark/codemirror-buttons","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samdark%2Fcodemirror-buttons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samdark%2Fcodemirror-buttons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samdark%2Fcodemirror-buttons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samdark%2Fcodemirror-buttons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samdark","download_url":"https://codeload.github.com/samdark/codemirror-buttons/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samdark%2Fcodemirror-buttons/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266563915,"owners_count":23948689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buttons","codemirror","ui"],"created_at":"2024-11-27T13:37:06.454Z","updated_at":"2025-07-22T20:05:15.243Z","avatar_url":"https://github.com/samdark.png","language":"JavaScript","funding_links":["https://github.com/sponsors/samdark","https://patreon.com/samdark"],"categories":[],"sub_categories":[],"readme":"CodeMirror buttons addon\n========================\n\nAdds a panel with buttons specified via config.\n\n\u003cimg src=\"screenshot.png\" /\u003e\n\n## Usage\n\nInclude scripts needed into webpage.\n\n```html\n\u003cscript src=\"bower_components/codemirror/lib/codemirror.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"bower_components/codemirror/addon/display/panel.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"bower_components/codemirror-buttons/buttons.js\"\u003e\u003c/script\u003e\n```\n\nInitialize CodeMirror specifying buttons as an array in `buttons` config property.\n\n```javascript\nvar editor = CodeMirror.fromTextArea(document.getElementById('text'), {\n\tmode: 'gfm',\n\tbuttons: [\n        {\n            hotkey: 'Ctrl-B',\n            class: 'bold',\n            label: '\u003cstrong\u003eB\u003c/strong\u003e',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                cm.replaceSelection('**' + selection + '**');\n                if (!selection) {\n                    var cursorPos = cm.getCursor();\n                    cm.setCursor(cursorPos.line, cursorPos.ch - 2);\n                }\n            }\n        },\n        {\n            hotkey: 'Ctrl-I',\n            class: 'italic',\n            label: '\u003ci\u003eI\u003c/i\u003e',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                cm.replaceSelection('*' + selection + '*');\n                if (!selection) {\n                    var cursorPos = cm.getCursor();\n                    cm.setCursor(cursorPos.line, cursorPos.ch - 1);\n                }\n            }\n        },\n        {\n            class: 'inline-code',\n            label: 'code',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                cm.replaceSelection(\"`\" + selection + \"`\");\n                if (!selection) {\n                    var cursorPos = cm.getCursor();\n                    cm.setCursor(cursorPos.line, cursorPos.ch - 1);\n                }\n            }\n        },\n        {\n            class: 'block-php',\n            label: '\u0026lt;php\u0026gt;',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                cm.replaceSelection(\"```php\\n\u003c?php\\n\" + selection + \"\\n```\\n\");\n                if (!selection) {\n                    var cursorPos = cm.getCursor();\n                    cm.setCursor(cursorPos.line - 2, 0);\n                }\n            }\n        },\n        {\n            class: 'block-code',\n            label: '\u0026lt;-\u0026gt;',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                cm.replaceSelection(\"```\\n\" + selection + \"\\n```\\n\");\n                if (!selection) {\n                    var cursorPos = cm.getCursor();\n                    cm.setCursor(cursorPos.line - 2, 0);\n                }\n            }\n        },\n        {\n            class: 'quote',\n            label: '\u003e',\n            callback: function (cm) {\n                cm.replaceSelection(\"\u003e \" + cm.getSelection());\n            }\n        },\n        {\n            class: 'ul',\n            label: 'ul',\n            callback: function (cm) {\n                cm.replaceSelection(\"- \" + cm.getSelection());\n            }\n        },\n        {\n            class: 'ol',\n            label: 'ol',\n            callback: function (cm) {\n                cm.replaceSelection(\"1. \" + cm.getSelection());\n            }\n        },\n        {\n            class: 'a',\n            label: 'a',\n            callback: function (cm) {\n                var selection = cm.getSelection();\n                var text = '';\n                var link = '';\n\n                if (selection.match(/^https?:\\/\\//)) {\n                    link = selection;\n                } else {\n                    text = selection;\n                }\n                cm.replaceSelection('[' + text + '](' + link + ')');\n\n                var cursorPos = cm.getCursor();\n                if (!selection) {\n                    cm.setCursor(cursorPos.line, cursorPos.ch - 3);\n                } else if (link) {\n                    cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length));\n                } else {\n                    cm.setCursor(cursorPos.line, cursorPos.ch - 1);\n                }\n            }\n        }\n    ],\n});\n```\n\nAltrnatively, instead of setting individual options, you can supply either DOM node or a callback returning DOM node\nusing `el` key:\n\n```javascript\n{\n    el: function(cm) {\n        return document.getElementById('mybutton');\n    }\n}\n```\n\nOptionally use stylesheet included to make buttons look a bit better:\n\n```html\n\u003clink rel=\"stylesheet\" href=\"bower_components/codemirror-buttons/buttons.css\"\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamdark%2Fcodemirror-buttons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamdark%2Fcodemirror-buttons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamdark%2Fcodemirror-buttons/lists"}