From 18306d3faf77a81166c685f1cfcd2f7b580a9c01 Mon Sep 17 00:00:00 2001 From: Muhammad Swalah A A Date: Wed, 7 Aug 2024 22:26:26 +0530 Subject: [PATCH 1/3] chore: setup node via volta --- package.json | 4 ++++ yarn.lock | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d12beb5..a111b76 100644 --- a/package.json +++ b/package.json @@ -43,5 +43,9 @@ }, "dependencies": { "@codexteam/icons": "^0.0.4" + }, + "volta": { + "node": "16.20.2", + "yarn": "1.22.22" } } diff --git a/yarn.lock b/yarn.lock index d6ab070..d9ab1f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1551,8 +1551,8 @@ camelcase@^5.0.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320" caniuse-lite@^1.0.30001280: - version "1.0.30001282" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001282.tgz#38c781ee0a90ccfe1fe7fefd00e43f5ffdcb96fd" + version "1.0.30001650" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001650.tgz" caseless@~0.12.0: version "0.12.0" From 477dc63dba727a6c7ff99083a8e1205aed25c12b Mon Sep 17 00:00:00 2001 From: Muhammad Swalah A A Date: Wed, 7 Aug 2024 23:19:33 +0530 Subject: [PATCH 2/3] feat: implement config.downloadButotn.onClick handler option for customizing onClick behaviour of the download button --- README.md | 49 +++++++++++++++++++++++++++++++++++++++ src/index.js | 12 ++++++++++ src/utils/customEvents.js | 13 +++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/utils/customEvents.js diff --git a/README.md b/README.md index b956e71..e49ee19 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Attaches Tool supports these configuration parameters: | buttonText | `string` | (default: `Select file`) Placeholder for file upload button | | errorMessage | `string` | (default: `File upload failed`) Message to show if file upload failed | | additionalRequestHeaders | `object` | (default: `{}`) Object with any custom headers which will be added to request. Example: `{"X-CSRF-TOKEN": "W5fe2...hR8d1"}` | +| downloadButton | `{onClick: function}` |Option custom onClick event handler to customize the onclick event of download icon| ## Output data @@ -176,3 +177,51 @@ var editor = EditorJS({ ... }); ``` + +## Providing custom download button event handlers + +As mentioned at the Config Params section, you have an ability to provide own custom onClick event handler for the download button. +It is a quite simple: implement `onClick` method and pass them via `downloadButton` config param. +This method will be invoked when the download button is clicked. + + +| Method | Arguments | Return value | Description | +| -------------- | --------- | -------------| ------------| +| onClick | `MouseEvent`, `Object` | `void` | Handle the click event for the download button, allowing custom logic | + +Example: + +```js +import AttachesTool from '@editorjs/attaches'; + +var editor = EditorJS({ + ... + + tools: { + ... + attaches: { + class: AttachesTool, + config: { + /** + * Custom download button handler + */ + downloadButton: { + /** + * Custom logic to execute when download button is clicked + * @param {MouseEvent} event - The click event object + * @param {Object} data - The file data object, containing information such as url, name, size, extension + */ + onClick: function(event, data) { + // Prevent the default download behavior + event.preventDefault(); + + // Custom download logic here + console.log('File download initiated:', data.file.url); + } + } + } + } + } + ... +}); +``` diff --git a/src/index.js b/src/index.js index 72061aa..1ec67c6 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ import Uploader from './uploader'; import { make, moveCaretToTheEnd, isEmpty } from './utils/dom'; import { getExtensionFromFileName } from './utils/file'; import { IconChevronDown, IconFile } from '@codexteam/icons'; +import {onClickDownloadButton} from './utils/customEvents'; const LOADER_TIMEOUT = 500; @@ -47,6 +48,8 @@ const LOADER_TIMEOUT = 500; * @property {string} errorMessage - message to show if file uploading failed * @property {object} [uploader] - optional custom uploader * @property {function(File): Promise.} [uploader.uploadByFile] - custom method that upload file and returns response + * @property {object} [downloadButton] - download button configuration + * @property {function(Event, AttachesToolData): void} [downloadButton.onClick] - custom method that handles download button click */ /** @@ -90,6 +93,7 @@ export default class AttachesTool { buttonText: config.buttonText || 'Select file to upload', errorMessage: config.errorMessage || 'File upload failed', uploader: config.uploader || undefined, + downloadButton: config.downloadButton || undefined, additionalRequestHeaders: config.additionalRequestHeaders || {}, }; @@ -444,6 +448,14 @@ export default class AttachesTool { rel: 'nofollow noindex noreferrer', }); + /** + * Registering the config.downloadButton.onClick handler. + * The function onClickDownloadButton will invoke the this.config.downloadButton.onClick function if it exists. + */ + downloadIcon.addEventListener('click', (event) =>{ + onClickDownloadButton(event, this.config, this.data); + }); + this.nodes.wrapper.appendChild(downloadIcon); } } diff --git a/src/utils/customEvents.js b/src/utils/customEvents.js new file mode 100644 index 0000000..4566d3b --- /dev/null +++ b/src/utils/customEvents.js @@ -0,0 +1,13 @@ +/** + * Registers the config.downloadButton.onClick function to the click event of the download button + * if it is defined. + * + * @param {MouseEvent} event - The click event. + * @param {Object} config - The configuration object. + * @param {AttachesToolData} data - The data object. + */ +export function onClickDownloadButton(event, config, data) { + if (config && config.downloadButton && config.downloadButton.onClick && typeof config.downloadButton.onClick === 'function') { + config.downloadButton.onClick(event, data); + } +} \ No newline at end of file From c7520dab09deeef1b47b382eb7fcd6fb40b1b67a Mon Sep 17 00:00:00 2001 From: Muhammad Swalah A A Date: Wed, 7 Aug 2024 23:47:02 +0530 Subject: [PATCH 3/3] chore(release): 1.3.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a111b76..8e67060 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/attaches", - "version": "1.3.0", + "version": "1.3.1", "repository": "https://github.com/editor-js/attaches", "license": "MIT", "scripts": {