Skip to content

Commit

Permalink
move clipboard.js to npm/webpack (go-gitea#10183)
Browse files Browse the repository at this point in the history
- created lazy-loaded webpack chunk for clipboard.js
- upgraded clipboard.js from 1.5.9 to 2.0.4
- parallelize initialization of all lazy-loaded features

Co-authored-by: Antoine GIRARD <[email protected]>
  • Loading branch information
silverwind and sapk authored Feb 7, 2020
1 parent 0754cec commit 4721d45
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 49 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ env:

globals:
__webpack_public_path__: true
Clipboard: false
CodeMirror: false
Dropzone: false
emojify: false
Expand Down
33 changes: 33 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"node": ">=10"
},
"dependencies": {
"clipboard": "2.0.4",
"fomantic-ui": "2.8.3",
"highlight.js": "9.18.1",
"jquery": "3.4.1",
Expand Down
4 changes: 2 additions & 2 deletions public/vendor/librejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
<td><a href="https://github.com/go-gitea/gitea/tree/master/web_src/js">*.js</a></td>
</tr>
<tr>
<td><a href="./plugins/clipboard/clipboard.min.js">clipboard.min.js</a></td>
<td><a href="../js/clipboard.js">clipboard.js</a></td>
<td><a href="http://www.freebsd.org/copyright/freebsd-license.html">Expat</a></td>
<td><a href="https://github.com/zenorocha/clipboard.js/archive/v1.5.9.tar.gz">clipboard-1.5.9.tar.gz</a></td>
<td><a href="https://github.com/zenorocha/clipboard.js/archive/v2.0.4.tar.gz">clipboard-2.0.4.tar.gz</a></td>
</tr>
<tr>
<td><a href="../js/gitgraph.js">gitgraph.js</a></td>
Expand Down
18 changes: 0 additions & 18 deletions public/vendor/plugins/clipboard/LICENSE

This file was deleted.

7 changes: 0 additions & 7 deletions public/vendor/plugins/clipboard/clipboard.min.js

This file was deleted.

1 change: 0 additions & 1 deletion templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
</script>
{{end}}
<script src="{{StaticUrlPrefix}}/vendor/plugins/emojify/emojify.custom.js"></script>
<script src="{{StaticUrlPrefix}}/vendor/plugins/clipboard/clipboard.min.js"></script>
<script src="{{StaticUrlPrefix}}/vendor/plugins/vue/vue.min.js"></script>
<script src="{{StaticUrlPrefix}}/fomantic/semantic.min.js?v={{MD5 AppVer}}"></script>
<script src="{{StaticUrlPrefix}}/js/index.js?v={{MD5 AppVer}}"></script>
Expand Down
2 changes: 1 addition & 1 deletion templates/pwa/serviceworker_js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var STATIC_CACHE = 'static-cache-v1';
var urlsToCache = [
// js
'{{StaticUrlPrefix}}/fomantic/semantic.min.js?v={{MD5 AppVer}}',
'{{StaticUrlPrefix}}/js/clipboard.js',
'{{StaticUrlPrefix}}/js/gitgraph.js',
'{{StaticUrlPrefix}}/js/highlight.js',
'{{StaticUrlPrefix}}/js/index.js?v={{MD5 AppVer}}',
'{{StaticUrlPrefix}}/js/jquery.js?v={{MD5 AppVer}}',
'{{StaticUrlPrefix}}/js/swagger.js?v={{MD5 AppVer}}',
'{{StaticUrlPrefix}}/vendor/plugins/clipboard/clipboard.min.js',
'{{StaticUrlPrefix}}/vendor/plugins/codemirror/addon/mode/loadmode.js',
'{{StaticUrlPrefix}}/vendor/plugins/codemirror/mode/meta.js',
'{{StaticUrlPrefix}}/vendor/plugins/dropzone/dropzone.js',
Expand Down
23 changes: 23 additions & 0 deletions web_src/js/features/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default async function initClipboard() {
const els = document.querySelectorAll('.clipboard');
if (!els || !els.length) return;

const { default: ClipboardJS } = await import(/* webpackChunkName: "clipboard" */'clipboard');

const clipboard = new ClipboardJS(els);
clipboard.on('success', (e) => {
e.clearSelection();

$(`#${e.trigger.getAttribute('id')}`).popup('destroy');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'));
$(`#${e.trigger.getAttribute('id')}`).popup('show');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'));
});

clipboard.on('error', (e) => {
$(`#${e.trigger.getAttribute('id')}`).popup('destroy');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'));
$(`#${e.trigger.getAttribute('id')}`).popup('show');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'));
});
}
23 changes: 4 additions & 19 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './vendor/semanticDropdown.js';
import initContextPopups from './features/contextPopup.js';
import initHighlight from './features/highlight.js';
import initGitGraph from './features/gitGraph.js';
import initClipboard from './features/clipboard.js';

import ActivityTopAuthors from './components/ActivityTopAuthors.vue';

Expand Down Expand Up @@ -2453,24 +2454,6 @@ $(document).ready(async () => {
}
}

// Clipboard JS
const clipboard = new Clipboard('.clipboard');
clipboard.on('success', (e) => {
e.clearSelection();

$(`#${e.trigger.getAttribute('id')}`).popup('destroy');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'));
$(`#${e.trigger.getAttribute('id')}`).popup('show');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'));
});

clipboard.on('error', (e) => {
$(`#${e.trigger.getAttribute('id')}`).popup('destroy');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'));
$(`#${e.trigger.getAttribute('id')}`).popup('show');
e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'));
});

// Helpers.
$('.delete-button').click(showDeletePopup);
$('.add-all-button').click(showAddAllPopup);
Expand Down Expand Up @@ -2579,7 +2562,6 @@ $(document).ready(async () => {
initRepoStatusChecker();
initTemplateSearch();
initContextPopups(suburl);
initGitGraph();

// Repo clone url.
if ($('#repo-clone-url').length > 0) {
Expand Down Expand Up @@ -2616,8 +2598,11 @@ $(document).ready(async () => {
}
});

// parallel init of lazy-loaded features
[hljs] = await Promise.all([
initHighlight(),
initGitGraph(),
initClipboard(),
]);
});

Expand Down

0 comments on commit 4721d45

Please sign in to comment.