Fix unresponsive copy button on instructions modal

This commit fixes the bug where the "Copy" button does not copy when
clicked on download instructions modal (on Linux and macOS).

This commit also introduces several improvements to the UI components
related to copy action and their interaction with the clipboard feature.

It adds more tests to avoid regression of the bugs and improves
maintainability, testability and adherence to Vue's reactive principles.

Changes include:

- Fix non-responsive copy button in the download instructions modal by
  triggering a `click` event in `AppIcon.vue`.
- Improve `TheCodeButtons.vue`:
  - Remove redundant `getCurrentCode` function.
  - Separate components for each button for better separation of
    concerns and higher testability.
  - Use the `gap` property in the flexbox layout, replacing the less
    explicit sibling combinator approach.
- Add `useClipboard` compositional hook for more idiomatic Vue approach
  to interacting with the clipboard.
- Add `useCurrentCode` compositional hook to handle current code state
  more effectively with unified logic.
- Abstract clipboard operations to an interface to isolate
  responsibilities.
- Switch clipboard implementation to the `navigator.clipboard` API,
  moving away from the deprecated `document.execCommand`.
- Move clipboard logic to the presentation layer to conform to
  separation of concerns and domain-driven design principles.
- Improve `IconButton.vue` component to increase reusability with
  consistent sizing.
This commit is contained in:
undergroundwires
2023-11-06 21:55:43 +01:00
parent b2ffc90da7
commit 8ccaec7af6
37 changed files with 881 additions and 206 deletions

View File

@@ -0,0 +1,15 @@
import { Clipboard } from './Clipboard';
export type NavigatorClipboard = typeof globalThis.navigator.clipboard;
export class BrowserClipboard implements Clipboard {
constructor(
private readonly navigatorClipboard: NavigatorClipboard = globalThis.navigator.clipboard,
) {
}
public async copyText(text: string): Promise<void> {
await this.navigatorClipboard.writeText(text);
}
}

View File

@@ -0,0 +1,3 @@
export interface Clipboard {
copyText(text: string): Promise<void>;
}

View File

@@ -0,0 +1,13 @@
import { FunctionKeys } from '@/TypeHelpers';
import { BrowserClipboard } from './BrowserClipboard';
import { Clipboard } from './Clipboard';
export function useClipboard(clipboard: Clipboard = new BrowserClipboard()) {
// Bind functions for direct use from destructured assignments such as `const { .. } = ...`.
const functionKeys: readonly FunctionKeys<Clipboard>[] = ['copyText'];
functionKeys.forEach((functionName) => {
const fn = clipboard[functionName];
clipboard[functionName] = fn.bind(clipboard);
});
return clipboard;
}

View File

@@ -0,0 +1,32 @@
import { ref } from 'vue';
import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode';
import { IEventSubscriptionCollection } from '@/infrastructure/Events/IEventSubscriptionCollection';
import { useCollectionState } from './UseCollectionState';
export function useCurrentCode(
state: ReturnType<typeof useCollectionState>,
events: IEventSubscriptionCollection,
) {
const { onStateChange } = state;
const currentCode = ref<string>('');
onStateChange((newState) => {
updateCurrentCode(newState.code.current);
subscribeToCodeChanges(newState.code);
}, { immediate: true });
function subscribeToCodeChanges(code: IApplicationCode) {
events.unsubscribeAllAndRegister([
code.changed.on((newCode) => updateCurrentCode(newCode.code)),
]);
}
function updateCurrentCode(newCode: string) {
currentCode.value = newCode;
}
return {
currentCode,
};
}

View File

@@ -1,5 +1,9 @@
<template>
<div v-html="svgContent" class="inline-icon" />
<div
class="inline-icon"
v-html="svgContent"
@click="onClicked"
/>
</template>
<script lang="ts">
@@ -18,10 +22,19 @@ export default defineComponent({
required: true,
},
},
setup(props) {
emits: [
'click',
],
setup(props, { emit }) {
const useSvgLoaderHook = inject('useSvgLoaderHook', useSvgLoader);
const { svgContent } = useSvgLoaderHook(() => props.icon);
return { svgContent };
function onClicked() {
emit('click');
}
return { svgContent, onClicked };
},
});