-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix the problem that the copy function is invalid
- Loading branch information
monster
committed
Jun 4, 2023
1 parent
3cc7adc
commit cfe5800
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/hamsterbase-highlighter/content-script/services/clipboard/browser/clipboardService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/hamsterbase-highlighter/content-script/services/clipboard/common/clipboardService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |