-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: toolbar and copy-to-clipboard plugin not work
fix #20
- Loading branch information
1 parent
e56b1f2
commit 30356e7
Showing
15 changed files
with
285 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PluginOptions } from '@/interface/plugin-options' | ||
import { RehypePrismPlugin } from '@/interface/rehype-prism-plugin' | ||
import { PluginContext } from './interface/plugin-context.js' | ||
import { createCopyToClipboardPlugin } from './plugins/copy-to-clipboard.js' | ||
import { createLineNumberPlugin } from './plugins/line-numbers.js' | ||
import { createToolbarPlugin } from './plugins/toolbar.js' | ||
|
||
type Applier = (options: PluginOptions) => void | ||
|
||
export function createPluginApplier(plugins: RehypePrismPlugin[]): Applier { | ||
const context: PluginContext = { | ||
toolbar: { | ||
buttons: [], | ||
}, | ||
} | ||
|
||
const appliers = plugins | ||
// uniq | ||
.filter((item, index, arr) => arr.indexOf(item, 0) === index) | ||
.map(plugin => { | ||
if (plugin === 'line-numbers') return createLineNumberPlugin() | ||
if (plugin === 'toolbar') return createToolbarPlugin(context) | ||
if (plugin === 'copy-to-clipboard') return createCopyToClipboardPlugin(context) | ||
}) | ||
|
||
return options => { | ||
for (const applier of appliers) { | ||
applier && applier(options) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Element } from 'hast' | ||
import { PluginOptions } from './plugin-options' | ||
|
||
|
||
export interface PluginContext { | ||
toolbar: { | ||
buttons: ((options: PluginOptions) => Element)[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { h } from 'hastscript' | ||
import { PluginContext } from '@/interface/plugin-context' | ||
|
||
interface Setting { | ||
'copy': 'Copy' | ||
'copy-error': 'Press Ctrl+C to copy' | ||
'copy-success': 'Copied!' | ||
'copy-timeout': 5000 | ||
} | ||
|
||
function createClickCallback(str: string, setting: Setting): string { | ||
return `(function(button){ | ||
const setting = ${JSON.stringify(setting)} | ||
const span = button.querySelector('span') | ||
function setState(state) { | ||
span.textContent = setting[state]; | ||
button.setAttribute('data-copy-state', state); | ||
} | ||
function resetButtonText() { | ||
setTimeout(function () { | ||
setState('copy'); | ||
}, setting['copy-timeout']); | ||
} | ||
function onSuccess() { | ||
setState('copy-success'); | ||
resetButtonText(); | ||
} | ||
function onError(error) { | ||
error && console.error(error) | ||
setState('copy-error'); | ||
resetButtonText(); | ||
} | ||
function fallbackCopyTextToClipboard(str) { | ||
var textArea = document.createElement('textarea'); | ||
textArea.value = str | ||
// Avoid scrolling to bottom | ||
textArea.style.top = '0'; | ||
textArea.style.left = '0'; | ||
textArea.style.position = 'fixed'; | ||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
try { | ||
var successful = document.execCommand('copy'); | ||
setTimeout(function () { | ||
if (successful) onSuccess() | ||
else onError() | ||
}, 1); | ||
} catch (err) { | ||
setTimeout(function () { | ||
onError(err) | ||
}, 1); | ||
} | ||
document.body.removeChild(textArea); | ||
} | ||
function copyTextToClipboard(str) { | ||
if (navigator.clipboard) { | ||
navigator.clipboard.writeText(str) | ||
.then(onSuccess, function () { | ||
fallbackCopyTextToClipboard(str); | ||
}); | ||
} else { | ||
fallbackCopyTextToClipboard(str); | ||
} | ||
} | ||
copyTextToClipboard(${JSON.stringify(str)}) | ||
})(this)` | ||
} | ||
|
||
export function createCopyToClipboardPlugin(context: PluginContext): void { | ||
context.toolbar.buttons.push(({ raw }) => { | ||
const span = h( | ||
'span', | ||
{}, | ||
['Copy'], | ||
) | ||
|
||
const copyBtn = h( | ||
'button', | ||
{ | ||
type: 'button', | ||
className: ['copy-to-clipboard-button'], | ||
onClick: createClickCallback(raw, { | ||
copy: 'Copy', | ||
'copy-error': 'Press Ctrl+C to copy', | ||
'copy-success': 'Copied!', | ||
'copy-timeout': 5000, | ||
}), | ||
}, | ||
[span], | ||
) | ||
|
||
return copyBtn | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
import { h } from 'hastscript' | ||
import { appendClassName } from '@/utils/append-class-name.js' | ||
import { Plugin } from '@/interface/plugin.js' | ||
import { appendClassName } from '@/utils/append-class-name.js' | ||
import { selectCodeElement } from '@/utils/select-code-element.js' | ||
|
||
|
||
function getLineNumber(str: string): number { | ||
const match = str.match(/\n(?!$)/g) | ||
return match ? match.length + 1 : 1 | ||
} | ||
|
||
export const applyLineNumberPlugin: Plugin = options => { | ||
const { preElement, codeElement, raw } = options | ||
export function createLineNumberPlugin(): Plugin { | ||
return options => { | ||
const { preElement, raw } = options | ||
|
||
const codeElement = selectCodeElement(preElement) | ||
if (!codeElement) return | ||
|
||
appendClassName(preElement, 'line-numbers') | ||
appendClassName(preElement, 'line-numbers') | ||
|
||
const lineNumber = getLineNumber(raw) | ||
const lineNumberColumn = h( | ||
'span', | ||
{ | ||
'aria-hidden': 'true', | ||
className: ['line-numbers-rows'], | ||
}, | ||
new Array(lineNumber).fill(h('span')), | ||
) | ||
codeElement.children.push(lineNumberColumn) | ||
const lineNumber = getLineNumber(raw) | ||
const lineNumberColumn = h( | ||
'span', | ||
{ | ||
'aria-hidden': 'true', | ||
className: ['line-numbers-rows'], | ||
}, | ||
new Array(lineNumber).fill(h('span')), | ||
) | ||
codeElement.children.push(lineNumberColumn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { h } from 'hastscript' | ||
import { Plugin } from '@/interface/plugin.js' | ||
import { PluginContext } from '@/interface/plugin-context.js' | ||
|
||
|
||
export function createToolbarPlugin(context: PluginContext): Plugin { | ||
return options => { | ||
const { parentNode, index, preElement } = options | ||
|
||
const toolbar = h( | ||
'div', | ||
{ | ||
className: ['toolbar'], | ||
}, | ||
) | ||
|
||
const container = h( | ||
'div', | ||
{ | ||
className: ['code-toolbar'], | ||
}, | ||
[preElement, toolbar], | ||
) | ||
|
||
parentNode.children.splice(index, 1, container) | ||
|
||
for (const button of context.toolbar.buttons) { | ||
toolbar.children.push(h( | ||
'div', | ||
{ | ||
className: ['toolbar-item'], | ||
}, | ||
[button(options)], | ||
)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Element } from 'hast' | ||
import { select } from 'unist-util-select' | ||
import { isElementNode } from './is-element-node.js' | ||
|
||
|
||
export function selectCodeElement(parent: Element): Element | null { | ||
const codeElement = select('[tagName=code]', parent) | ||
return isElementNode(codeElement) ? codeElement : null | ||
} |
Oops, something went wrong.