Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Updating manifest & various configs to contain the correct version. #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ You may have to reload obsidian (`ctrl+R`) to see changes.

# Version History

## v0.2.1
Forked project to update manifest.json

## v0.2.0
Add excluded languages, currently only includes "todoist" to be compatible with the todoist plugin. Will add setting page to add custom exclude languages in future.

Expand Down
57 changes: 31 additions & 26 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const excludeLangs = [
"todoist"
];

const svgCopy = '<svg viewBox="0 0 100 100" width="20" height="20" class="documents"><path fill="currentColor" stroke="currentColor" d="M74,4c-0.1,0-0.2,0-0.3,0H40v11.5l4,4V8h28v20h20v48H64v4h32V26.3c0-0.2,0-0.4,0-0.6v-0.5l-0.4-0.4 c-0.1-0.1-0.2-0.3-0.4-0.4c0,0,0,0,0,0L75.6,4.8c-0.1-0.1-0.2-0.3-0.4-0.4L74.8,4h-0.5C74.2,4,74.1,4,74,4L74,4z M76,10.8L89.2,24 H76V10.8z M38,20c-0.1,0-0.2,0-0.3,0H4v76h56V42.3c0-0.2,0-0.4,0-0.6v-0.5l-0.4-0.4c-0.1-0.1-0.2-0.3-0.4-0.4L39.6,20.7 c-0.1-0.1-0.2-0.3-0.4-0.4L38.8,20h-0.5C38.2,20,38.1,20,38,20z M8,24h28v20h20v48H8L8,24z M40,26.8L53.2,40H40V26.8z M60.5,36 l4,4H84v-4L60.5,36z M64,48v4h12v-4H64z M16,52v4h32v-4H16z M64,60v4h20v-4H64z M16,64v4h24v-4H16z M16,76v4h32v-4H16z"></path></svg>';


export default class CMSyntaxHighlightPlugin extends Plugin {

Expand All @@ -26,42 +28,45 @@ export default class CMSyntaxHighlightPlugin extends Plugin {
}

addCopyButtons(clipboard:any) {
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
document.querySelectorAll('pre > code').forEach(function (codeBlock: HTMLElement) {

var pre = codeBlock.parentNode;
const pre = codeBlock.parentNode as HTMLPreElement;

// check for excluded langs
for ( let lang of excludeLangs ){
if (pre.classList.contains( `language-${lang}` ))
if (pre.classList.contains(`language-${lang}`))
return;
}

// Dont add more than once
if (pre.parentNode.classList.contains('has-copy-button')) {
const parent = pre.parentNode as HTMLDivElement;

// don't add more than once
if (parent.classList.contains('has-copy-button')) {
return;
}
pre.parentNode.classList.add( 'has-copy-button' );

var button = document.createElement('button');
button.className = 'copy-code-button';
button.type = 'button';
button.innerText = 'Copy';

button.addEventListener('click', function () {
clipboard.writeText(codeBlock.innerText).then(function () {
/* Chrome doesn't seem to blur automatically,
leaving the button in a focused state. */
button.blur();

button.innerText = 'copied!';

setTimeout(function () {
button.innerText = 'copy';
}, 2000);
}, function (error) {
button.innerText = 'Error';
});
});
parent.classList.add('has-copy-button');

const button = document.createElement('div');
button.className = 'copy-code-button';
button.setAttribute('aria-label', 'Copy code block');
button.innerHTML = svgCopy;

button.addEventListener('click', function () {
clipboard.writeText(codeBlock.innerText).then(function () {
/* Chrome doesn't seem to blur automatically,
leaving the button in a focused state. */
button.blur();

button.innerText = 'Code has been copied!';

setTimeout(function () {
button.innerHTML = svgCopy;
}, 2000);
}, function () {
button.innerText = 'An error occurred!';
});
});

pre.appendChild(button);

Expand Down
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "code-block-copy",
"name": "Copy button for code blocks",
"name": "Copy button for code blocks (fixed version manifest)",
"author": "Daniel Brandenburg",
"description": "Copy button for code blocks",
"description": "Temporary version fix released by Nathaniel Palmer of source project by Daniel Brandenburg (https:\/\/github.com\/jdbrice\/obsidian-code-block-copy)",
"isDesktopOnly": false,
"version": "0.2.0"
"version": "0.2.1"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "code-block-copy",
"version": "0.1.0",
"version": "0.2.1",
"description": "Copy button for code blocks",
"main": "main.js",
"scripts": {
Expand Down
26 changes: 8 additions & 18 deletions styles.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

.copy-code-button {
color: var(--background-primary);
background-color: var(--text-faint);
border-radius: 1px 1px 0px 0px;
color: var(--text-faint);

/* right-align */
display: block;
Expand All @@ -16,23 +14,15 @@
position: absolute;
top: 0px;
right: 0px;
}

.copy-code-button:hover {
cursor: pointer;
background-color: var(--text-normal);
}

.copy-code-button:focus {
/* Avoid an ugly focus outline on click in Chrome,
but darken the button for accessibility.
See https://stackoverflow.com/a/25298082/1481479 */
background-color: var(--text-normal);
outline: 0;
}
&:hover, &:active {
cursor: pointer;
color: var(--interactive-accent-hover);
}

.copy-code-button:active {
background-color: var(--text-normal);
&:focus {
outline: 0;
}
}

.highlight pre {
Expand Down