Skip to content

Commit

Permalink
fix: Fix the problem that the copy function is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
monster committed Jun 4, 2023
1 parent 3cc7adc commit cfe5800
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IClipboardService } from "@/content-script/services/clipboard/common/clipboardService";
import { localize } from "@/locales/nls";
import CopyOne from "@icon-park/react/es/icons/CopyOne";
import HighLight from "@icon-park/react/es/icons/HighLight";
Expand Down Expand Up @@ -88,6 +89,7 @@ export const TakeNote: React.FC<{

export const HighlightTool = () => {
const HighlightMenuService = useService(IHighlightMenuService);
const clipboardService = useService(IClipboardService);
useEventRender(HighlightMenuService.onControllerChange);
useEventRender(HighlightMenuService.controller?.onStatusChange);

Expand Down Expand Up @@ -138,6 +140,15 @@ export const HighlightTool = () => {
controller.highlightAndTakeNote();
break;
}
case "copy": {
if (controller.state.type === "open_highlight_toolbar") {
clipboardService.writeText(
controller.state.option.range.text
);
controller.dispose();
}
break;
}
}
e.preventDefault();
e.stopPropagation();
Expand Down
10 changes: 8 additions & 2 deletions src/hamsterbase-highlighter/content-script/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { WebpageService } from "@/content-script/services/webpage/browser/webpag
import { IWebpageService } from "@/content-script/services/webpage/common/webpage-service";
import "@/locales/nls";
import { localize } from "@/locales/nls";
import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import React from "react";
import ReactDOM from "react-dom";
import {
Expand All @@ -33,8 +35,8 @@ import {
import { App } from "./app";
import { HamsterbaseHighlighterContext } from "./context";
import { HighlightController } from "./controller/highlight-controller";
import { CacheProvider } from "@emotion/react";
import createCache from "@emotion/cache";
import { BrowserClipboardService } from "./services/clipboard/browser/clipboardService";
import { IClipboardService } from "./services/clipboard/common/clipboardService";

class Main {
async load() {
Expand Down Expand Up @@ -195,6 +197,10 @@ class Main {
private initServices(): IInstantiationService {
const serviceCollection = new ServiceCollection();
serviceCollection.set(ISettingService, new SyncDescriptor(ChromeStorage));
serviceCollection.set(
IClipboardService,
new SyncDescriptor(BrowserClipboardService)
);
serviceCollection.set(INativeService, new SyncDescriptor(NativeService));
serviceCollection.set(
IHighlightMenuService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Disposable } from "vscf/base/common/lifecycle";
import { IClipboardService } from "@/content-script/services/clipboard/common/clipboardService";

export class BrowserClipboardService
extends Disposable
implements IClipboardService
{
declare readonly _serviceBrand: undefined;

constructor() {
super();
}

async writeText(text: string): Promise<void> {
try {
return await navigator.clipboard.writeText(text);
} catch (error) {
console.error(error);
}

const activeElement = document.activeElement;
const element = document.createElement("textarea");
element.setAttribute("aria-hidden", "true");
const textArea: HTMLTextAreaElement = document.body.appendChild(element);
textArea.style.height = "1px";
textArea.style.width = "1px";
textArea.style.position = "absolute";

textArea.value = text;
textArea.focus();
textArea.select();

document.execCommand("copy");

if (activeElement instanceof HTMLElement) {
activeElement.focus();
}

document.body.removeChild(textArea);

return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { createDecorator } from "vscf/platform/instantiation/common";

export const IClipboardService =
createDecorator<IClipboardService>("clipboardService");

export interface IClipboardService {
readonly _serviceBrand: undefined;

/**
* Writes text to the system clipboard.
*/
writeText(text: string): Promise<void>;
}

0 comments on commit cfe5800

Please sign in to comment.