Skip to content

Commit

Permalink
Toolbar: Minor improvements (#1818)
Browse files Browse the repository at this point in the history
The `registerButton` function will now log a warning and do nothing if a button is registered twice.
  • Loading branch information
RunDevelopment authored Mar 24, 2019
1 parent 191830f commit 3ad4704
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion plugins/toolbar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>How to use</h1>
<code>Prism.plugins.toolbar.registerButton</code>.</p>

<p>The function accepts a key for the button and an object with a <code>text</code> property string and an optional
<code>onClick</code> function or <code>url</code> string. The <code>onClick</code> function will be called when the button is clicked, while the
<code>onClick</code> function or a <code>url</code> string. The <code>onClick</code> function will be called when the button is clicked, while the
<code>url</code> property will be set to the anchor tag's <code>href</code>.</p>

<pre><code class="language-javascript">Prism.plugins.toolbar.registerButton('hello-world', {
Expand Down
14 changes: 13 additions & 1 deletion plugins/toolbar/prism-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@

Prism.plugins.toolbar = {};

/**
* @typedef ButtonOptions
* @property {string} text The text displayed.
* @property {string} [url] The URL of the link which will be created.
* @property {Function} [onClick] The event listener for the `click` event of the created button.
*/

/**
* Register a button callback with the toolbar.
*
* @param {string} key
* @param {Object|Function} opts
* @param {ButtonOptions|Function} opts
*/
var registerButton = Prism.plugins.toolbar.registerButton = function (key, opts) {
var callback;
Expand Down Expand Up @@ -43,6 +50,11 @@
};
}

if (key in map) {
console.warn('There is a button with the key "' + key + '" registered already.');
return;
}

callbacks.push(map[key] = callback);
};

Expand Down
2 changes: 1 addition & 1 deletion plugins/toolbar/prism-toolbar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ad4704

Please sign in to comment.