Skip to content

Commit

Permalink
feat: force copy onpaste event
Browse files Browse the repository at this point in the history
  • Loading branch information
WindRunnerMax committed Dec 20, 2024
1 parent 9ab59bf commit 51f493d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/copy/src/constant/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export const FOCUS = "focus";
export const BLUR = "blur";
export const FOCUS_IN = "focusin";
export const FOCUS_OUT = "focusout";
export const PASTE = "paste";
3 changes: 3 additions & 0 deletions packages/force-copy/src/bridge/content-inject/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const CI_REQUEST_ENUM = [
"DEBUG_MOUSE_EVENT",
"DEBUG_FOCUS_EVENT",
"DEBUG_EDITABLE",
"DEBUG_PASTE",
] as const;

export const CONTENT_TO_INJECT_REQUEST = CI_REQUEST_ENUM.reduce(
(acc, cur) => ({ ...acc, [cur]: `__${cur}__${MARK}__` }),
{} as { [K in typeof CI_REQUEST_ENUM[number]]: `__${K}__${typeof MARK}__` }
Expand All @@ -28,6 +30,7 @@ export type EventMap = {
[CONTENT_TO_INJECT_REQUEST.DEBUG_MOUSE_EVENT]: null;
[CONTENT_TO_INJECT_REQUEST.DEBUG_FOCUS_EVENT]: null;
[CONTENT_TO_INJECT_REQUEST.DEBUG_EDITABLE]: null;
[CONTENT_TO_INJECT_REQUEST.DEBUG_PASTE]: null;
};

export type CIRequestType = Reflex.Tuple<EventMap>;
3 changes: 3 additions & 0 deletions packages/force-copy/src/bridge/popup-content/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const PC_REQUEST_TYPE = [
"DEBUG_MOUSE_EVENT",
"DEBUG_FOCUS_EVENT",
"DEBUG_EDITABLE",
"DEBUG_PASTE_EVENT",
] as const;

export const POPUP_TO_CONTENT_REQUEST = PC_REQUEST_TYPE.reduce(
(acc, cur) => ({ ...acc, [cur]: `__${cur}__${MARK}__` }),
{} as { [K in typeof PC_REQUEST_TYPE[number]]: `__${K}__${typeof MARK}__` }
Expand All @@ -23,6 +25,7 @@ type EventMap = {
[POPUP_TO_CONTENT_REQUEST.DEBUG_MOUSE_EVENT]: null;
[POPUP_TO_CONTENT_REQUEST.DEBUG_FOCUS_EVENT]: null;
[POPUP_TO_CONTENT_REQUEST.DEBUG_EDITABLE]: null;
[POPUP_TO_CONTENT_REQUEST.DEBUG_PASTE_EVENT]: null;
};

export type PCRequestType = Reflex.Tuple<EventMap>;
7 changes: 7 additions & 0 deletions packages/force-copy/src/content/channel/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,12 @@ export const onPopupMessage = (data: PCRequestType) => {
});
break;
}
case PCBridge.REQUEST.DEBUG_PASTE_EVENT: {
CIBridge.postToInject({
type: CIBridge.REQUEST.DEBUG_PASTE,
payload: null,
});
break;
}
}
};
4 changes: 4 additions & 0 deletions packages/force-copy/src/inject/channel/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export const onContentMessage = (handler: WebSite) => {
document.body.contentEditable = "true";
break;
}
case CIBridge.REQUEST.DEBUG_PASTE: {
EventBus.on(EVENTS_ENUM.PASTE_CAPTURE, stopNativePropagation);
break;
}
}
};
};
2 changes: 2 additions & 0 deletions packages/force-copy/src/inject/utils/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const EVENTS_TYPE = [
"MOUSE_MOVE_CAPTURE",
"TOUCH_MOVE_CAPTURE",
"TOUCH_END_CAPTURE",
"PASTE_CAPTURE",
] as const;

export const EVENTS_ENUM = EVENTS_TYPE.reduce(
Expand Down Expand Up @@ -48,6 +49,7 @@ interface EventBusParams {
[EVENTS_ENUM.BLUR_CAPTURE]: FocusEvent;
[EVENTS_ENUM.TOUCH_MOVE_CAPTURE]: TouchEvent;
[EVENTS_ENUM.TOUCH_END_CAPTURE]: TouchEvent;
[EVENTS_ENUM.PASTE_CAPTURE]: ClipboardEvent;
}

declare module "laser-utils/dist/es/event-bus" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@ $box-shadow: 0 0 4px var(--color-border-2);
border-radius: 4px;
box-shadow: $box-shadow;

.row {
margin-bottom: 5px;
&:last-child {
margin-bottom: auto;
}
}
}
34 changes: 22 additions & 12 deletions packages/force-copy/src/popup/components/tools/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import type { FC } from "react";
import type { I18n } from "@/popup/i18n";
import { PCBridge } from "@/bridge/popup-content";

const Row = Grid.Row;
const Col = Grid.Col;
const GridItem = Grid.GridItem;

export const Tools: FC<{
i18n: I18n;
visible?: boolean;
}> = ({ i18n }) => {
return (
<div className={styles.container}>
<Row className={styles.row} gutter={10}>
<Col span={12}>
<Grid cols={2} colGap={10} rowGap={5}>
<GridItem>
<Button
size="mini"
onClick={() =>
Expand All @@ -26,8 +25,8 @@ export const Tools: FC<{
>
{i18n.t("Tools.MouseEvent")}
</Button>
</Col>
<Col span={12}>
</GridItem>
<GridItem>
<Button
onClick={() =>
PCBridge.postToContent({
Expand All @@ -39,10 +38,8 @@ export const Tools: FC<{
>
{i18n.t("Tools.FocusEvent")}
</Button>
</Col>
</Row>
<Row className={styles.row}>
<Col span={12}>
</GridItem>
<GridItem>
<Button
onClick={() =>
PCBridge.postToContent({
Expand All @@ -54,8 +51,21 @@ export const Tools: FC<{
>
{i18n.t("Tools.Editable")}
</Button>
</Col>
</Row>
</GridItem>
<GridItem>
<Button
onClick={() =>
PCBridge.postToContent({
type: PCBridge.REQUEST.DEBUG_PASTE_EVENT,
payload: null,
})
}
size="mini"
>
{i18n.t("Tools.PasteEvent")}
</Button>
</GridItem>
</Grid>
</div>
);
};
1 change: 1 addition & 0 deletions packages/force-copy/src/popup/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export const en = {
MouseEvent: "MouseEvent",
FocusEvent: "FocusEvent",
Editable: "Editable",
PasteEvent: "PasteEvent",
},
};

0 comments on commit 51f493d

Please sign in to comment.