Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy to clipboard for code blocks #963

Merged
merged 49 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
72d4cef
Update
zarkosimic Sep 18, 2019
dff04c5
Merge remote-tracking branch 'upstream/master'
zarkosimic Sep 20, 2019
f19e8aa
Merge remote-tracking branch 'upstream/master'
zarkosimic Sep 24, 2019
1deffe2
Merge remote-tracking branch 'upstream/master'
zarkosimic Sep 25, 2019
69b33fc
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 1, 2019
ad2cbfb
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 8, 2019
9afc6b1
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 9, 2019
f969fba
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 24, 2019
5e7c452
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 25, 2019
06ba748
Merge remote-tracking branch 'upstream/master'
zarkosimic Oct 31, 2019
589fad6
initial styles
marynaKhromova Nov 6, 2019
ed1b2aa
content styles
marynaKhromova Nov 7, 2019
846f861
Docu app changes
zarkosimic Nov 7, 2019
16c0ee2
responsive styles
marynaKhromova Nov 7, 2019
31d3dfb
Merge branch 'docu-app-changes' into 853-Create-styles-for-documentation
zarkosimic Nov 8, 2019
105a474
fix for safari
marynaKhromova Nov 8, 2019
24f0bd9
Merge branch '853-Create-styles-for-documentation' of github.com:mary…
marynaKhromova Nov 8, 2019
38a191b
Merge remote-tracking branch 'upstream/master' into 853-Create-styles…
marynaKhromova Nov 8, 2019
516fae9
Linking to pages fix for prod
zarkosimic Nov 11, 2019
3337dd4
Merge branch '853-Create-styles-for-documentation' of github.com:mary…
zarkosimic Nov 11, 2019
c1efa7b
Merge branch '853-Create-styles-for-documentation' of github.com:mary…
maxmarkus Nov 11, 2019
c08542c
Internal navigation with Luigi Client working, prepared for Anchor links
maxmarkus Nov 12, 2019
b176d8d
same page anchor links are working
maxmarkus Nov 12, 2019
e353426
fix for internal relative links starting with ./
maxmarkus Nov 12, 2019
bdaa864
fix for relative raw links, cleanup
maxmarkus Nov 12, 2019
523d5cc
added custom code inside pre block and js to copy the code
maxmarkus Nov 12, 2019
a10d710
css styling
maxmarkus Nov 12, 2019
1aecf54
Merge remote-tracking branch 'upstream/master' into 919-dynamic-inter…
maxmarkus Nov 13, 2019
a68790a
update gitignore
maxmarkus Nov 13, 2019
5c35e1f
cleanup
maxmarkus Nov 13, 2019
d03b791
Merge branch '919-dynamic-internal-linking' into 962-copy-to-clipboard
maxmarkus Nov 13, 2019
3b936c8
Merge remote-tracking branch 'upstream/master' into 962-copy-to-clipb…
maxmarkus Nov 13, 2019
0bb65cd
Merge remote-tracking branch 'upstream/master' into 919-dynamic-inter…
maxmarkus Nov 13, 2019
9e405e5
added missing images
maxmarkus Nov 13, 2019
1dbdd90
fixed copy-clipboard image
maxmarkus Nov 13, 2019
f72668d
Merge branch 'master' into 919-dynamic-internal-linking
zarkosimic Nov 13, 2019
ac32e01
Merge branch '919-dynamic-internal-linking' of github.com:maxmarkus/l…
maxmarkus Nov 13, 2019
6b8f523
added issing config example node
maxmarkus Nov 13, 2019
9c13ec8
fixed some wrong anchors
maxmarkus Nov 13, 2019
5f2efab
added missing config step to export
maxmarkus Nov 13, 2019
9c750eb
fix for export
maxmarkus Nov 14, 2019
485a614
fix for internal urls
maxmarkus Nov 14, 2019
89d51a4
parent container for copybutton
maxmarkus Nov 14, 2019
716c555
removed dev helper
maxmarkus Nov 15, 2019
0ede536
css adjustments for mobile resolution
marynaKhromova Nov 15, 2019
5834b1e
Merge branch '962-copy-to-clipboard' of github.com:maxmarkus/luigi in…
maxmarkus Nov 15, 2019
da00826
Merge branch '919-dynamic-internal-linking' into 962-copy-to-clipboard
maxmarkus Nov 15, 2019
3e49c30
Merge remote-tracking branch 'upstream/master' into 962-copy-to-clipb…
maxmarkus Nov 15, 2019
a7160ad
Merge remote-tracking branch 'upstream/master' into 962-copy-to-clipb…
maxmarkus Nov 18, 2019
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
12 changes: 7 additions & 5 deletions website/docs/scripts/markdown.unified-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { readdirSync, readFileSync } from 'fs';

const filesToProcess = [
// 'plain.md',
// 'codeblocks.md',
'links.md',
// 'link.md',
];

filesToProcess.forEach(async (name) => {
const fileContent = readFileSync(__dirname + '/mocks/' + name);
const result = await MarkdownSvc.process(String(fileContent), { shortName: name.replace('.md', '') });
console.log(`============== ${name} =================`);
console.log(result)
console.log(`============ end: ${name} ==============`);
console.log('');
MarkdownSvc.process(String(fileContent), { shortName: name.replace('.md', '') }).then(result => {
console.log(`============== ${name} =================`);
console.log(result)
console.log(`============ end: ${name} ==============`);
console.log('');
}).catch(e => console.error);
});
13 changes: 13 additions & 0 deletions website/docs/scripts/mocks/codeblocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
```javascript
const something = () => {}
```

```
some other plain code
```

```bash
bash code
```

`inline code` should not be copied
29 changes: 28 additions & 1 deletion website/docs/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,31 @@ LuigiClient.addInitListener((ctx) => {
}
});
}
});
});

const selectText = (node) => {
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
} else {
console.warn("Could not select text in node: Unsupported browser.");
}
}

window.copyCode = (evt, elem) => {
evt.preventDefault();
evt.stopPropagation();
try {
selectText(elem.parentNode.querySelector('code'));
document.execCommand('copy');
} catch(e) {
console.error('Browser copy command not supported?', e);
}
}
94 changes: 93 additions & 1 deletion website/docs/src/routes/_layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,104 @@
}
}

.copyCodeContainer {
position: relative;
}

.copyCode {
position: absolute;
right: -10px;
top: -10px;
width: 28px;
height: 28px;
border-radius: 50%;
background-color: $primary-color;
border: 1px $primary-color solid;
box-shadow: 0 0 4px 0 $primary-color;
cursor: pointer;
& > div {
position: relative;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}
img {
width: 13px;
height: 16px;
}
.popoverCopy {
position: absolute;
right: 0;
bottom: -37px;
font-family: 'Open Sans', sans-serif;
display: none;
font-size: 11px;
line-height: 13px;
color: $secondary-color;
background-color: white;
border: 1px $primary-color solid;
padding: 5px 11px;
white-space: nowrap;

// Arrow needs to be added
&:before,
&:after {
content: ' ';
position: absolute;
bottom: 100%;
width: 0;
height: 0;
}

&:before {
right: 2px;
border: 10px solid transparent;
border-bottom-color: $primary-color;
}

// cutout
&:after {
right: 3px;
border: 9px solid transparent;
border-bottom-color: white;
background-color: transparent;
}

@media screen and (min-width: (1024px - $side-nav-width)) {

left: 50%;
right: auto;
transform: translateX(-50%);

&:before {
left: 34px;
right: auto;
}

&:after {
left: 35px;
right: auto;
}
}
}
&:hover {
background-color: white;
border-color: $primary-color;
.popoverCopy {
display: block;
}
}
}

pre {
width: 100%;
display: block;
overflow-x: auto;
width: 1px;
min-width: 100%;
*width: 100%;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
border: 1px solid $powder-blue;
background-color: rgba(237, 242, 247, 0.1);
Expand All @@ -168,6 +259,7 @@
margin-bottom: 40px;

code {
overflow-x: auto;
word-break: normal;
white-space: pre;
overflow-wrap: normal;
Expand Down
9 changes: 7 additions & 2 deletions website/docs/src/services/markdown.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import html from 'rehype-stringify';
import addIdsToHeadings from 'rehype-slug';

import luigiLinkParser from '../unified-plugins/rehype-luigi-linkparser';
import addCopyToClipboard from '../unified-plugins/rehype-copy-to-clipboard';

// import highlight from 'rehype-highlight' // syntax highlight code blocks with lowlight: https://github.com/wooorm/lowlight
import rehypeSection from '@agentofuser/rehype-section';
const section = rehypeSection.default;

class MarkdownService {
async process(value, data = {}) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
unified()
.use(markdown)
.use(remark2rehype, {allowDangerousHTML: true})
Expand All @@ -23,10 +24,14 @@ class MarkdownService {
.use(section)
// .use(highlight)
.use(addIdsToHeadings)
.use(addCopyToClipboard)
.use(format)
.use(html)
.process(String(value), function (err, file) {
// console.error(report(err || file))
if(err) {
console.error(err || file)
return reject();
}
resolve(file.contents);
});
});
Expand Down
8 changes: 8 additions & 0 deletions website/docs/src/unified-plugins/plugin-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export function prependForExport() {
if (process.env.NODE_ENV == 'production') {
return '/docu-microfrontend';
} else {
return '';
}
}
30 changes: 30 additions & 0 deletions website/docs/src/unified-plugins/rehype-copy-to-clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import h from 'hastscript';
import visit from 'unist-util-visit';
import { prependForExport } from './plugin-helpers';

export default function addCopyToClipboard() {
return function transformer(tree) {
visit(tree, 'element', function (node) {
modify(node, 'code');
});
}

function modify(node, prop) {
const notYetProcessed = !node.properties.className || node.properties.className.indexOf('canCopyCode') === -1; // prevent infinite loop
if (node.tagName === 'pre' && notYetProcessed) {
// Docu: https://github.com/syntax-tree/hastscript#use
const newNodeData = h('div.copyCodeContainer', [
h('a.copyCode', { onclick: 'copyCode(event, this)' },
[h('div', [
h('img', { src: prependForExport() + '/images/copy-clipboard-default.svg' }),
h('.popoverCopy', [
'Click to copy'
])]
)]
),
h('pre.canCopyCode', node.children)
]);
Object.assign(node, newNodeData);
}
}
}
16 changes: 5 additions & 11 deletions website/docs/src/unified-plugins/rehype-luigi-linkparser.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import has from 'hast-util-has-property';
import url from 'url';
import visit from 'unist-util-visit';
import fs from 'fs';
import { writeFileSync, appendFileSync } from 'fs';
import { prependForExport } from './plugin-helpers';

let log = () => {}
if (process.env.NODE_ENV === 'debug') {
const debugFile = __dirname + '/debug.log';
fs.writeFileSync(debugFile, '');
writeFileSync(debugFile, '');
log = (text = '') => {
fs.appendFileSync(debugFile, text);
appendFileSync(debugFile, text);
}
}

export default function luigiLinkParser(options) {
var settings = options || {};

return function transformer(tree) {
visit(tree, 'element', function (node) {
modify(node, 'href');
});
}

function prependForExport() {
if (process.env.NODE_ENV == 'production') {
return '/docu-microfrontend';
} else {
return '';
}
}

function modify(node, prop) {
const githubMaster = 'https://github.com/SAP/luigi/blob/master/';
if (has(node, prop)) {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/static/images/copy-clipboard-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions website/docs/static/images/icon_alert.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions website/docs/static/images/icon_info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.