prism-code-editor
Version:
Lightweight, extensible code editor component for the web using Prism
32 lines (31 loc) • 1.6 kB
JavaScript
import { i as doc, r as createTemplate, t as addListener } from "../../core-E7btWBqK.js";
import { m as setSelection, t as addOverlay } from "../../utils-BffvWiz1.js";
//#region src/extensions/copyButton/index.ts
/** @module copy-button */
/**
* Creates a static copy button that can be added to the overlays of an editor or code
* block. The `firstChild` of the element returned is the button itself.
*/
var createCopyButton = /* @__PURE__ */ createTemplate("<div style=display:flex;align-items:flex-start;justify-content:flex-end><button type=button dir=ltr style=display:none class=pce-copy aria-label=Copy><svg width=1.2em aria-hidden=true viewBox=\"0 0 16 16\" overflow=visible stroke-linecap=round fill=none stroke=currentColor><rect x=4 y=4 width=11 height=11 rx=1 /><path d=\"m12 2a1 1 0 00-1-1H2A1 1 0 001 2v9a1 1 0 001 1\">");
/**
* Extension that adds a copy button to the editor.
* Probably best used with a read-only editor.
* You must also import styles from `prism-code-editor/copy-button.css`.
*/
var copyButton = () => (editor) => {
const container = createCopyButton();
const btn = container.firstChild;
addListener(btn, "click", () => {
btn.setAttribute("aria-label", "Copied!");
if (!navigator.clipboard?.writeText(editor.extensions.codeFold?.fullCode ?? editor.value)) {
editor.textarea.select();
doc.execCommand("copy");
setSelection(editor, 0);
}
});
addListener(btn, "pointerenter", () => btn.setAttribute("aria-label", "Copy"));
addOverlay(editor, container);
};
//#endregion
export { copyButton, createCopyButton };
//# sourceMappingURL=index.js.map