Skip to content

Commit 68c2277

Browse files
authored
feat(composer): fold large pastes into text attachments (pingdotgg#11442)
1 parent 36caf20 commit 68c2277

77 files changed

Lines changed: 3275 additions & 462 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
openExternal,
4141
openSystemSettings,
4242
checkSystemPermission,
43+
pasteAsText,
4344
probeRemoteEditors,
4445
pickFolder,
4546
pickProjectFavicon,
@@ -124,6 +125,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
124125
yield* ipc.handle(openExternal);
125126
yield* ipc.handle(openSystemSettings);
126127
yield* ipc.handle(checkSystemPermission);
128+
yield* ipc.handle(pasteAsText);
127129
yield* ipc.handle(probeRemoteEditors);
128130
yield* ipc.handle(getUpdateState);
129131
yield* ipc.handle(setUpdateChannel);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
77
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
88
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
99
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
10+
export const PASTE_AS_TEXT_CHANNEL = "desktop:paste-as-text";
1011
export const SNAP_SHOT_EVENT_CHANNEL = "desktop:snap-shot-event";
1112
export const QUIT_SHORTCUT_CHANNEL = "desktop:quit-shortcut";
1213
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ import { vi } from "vite-plus/test";
66

77
import type * as Electron from "electron";
88

9+
const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
10+
focusedWebContents: vi.fn(),
11+
ownerWindow: vi.fn(),
12+
}));
13+
vi.mock("electron", () => ({
14+
webContents: { getFocusedWebContents: focusedWebContents },
15+
BrowserWindow: { fromWebContents: ownerWindow },
16+
}));
17+
918
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
1019
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
1120
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
1221
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
1322
import {
1423
getLocalEnvironmentBootstraps,
1524
getWindowFullscreenState,
25+
pasteAsText,
1626
pickProjectFavicon,
1727
} from "./window.ts";
1828

@@ -153,6 +163,50 @@ describe("getWindowFullscreenState", () => {
153163
});
154164
});
155165

166+
describe("pasteAsText", () => {
167+
it.effect(
168+
"pastes into the focused guest only after the main renderer acknowledges the menu action",
169+
() => {
170+
const paste = vi.fn();
171+
const mainPaste = vi.fn();
172+
const window = {
173+
webContents: { id: 42, paste: mainPaste },
174+
isDestroyed: () => false,
175+
} as unknown as Electron.BrowserWindow;
176+
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => false });
177+
ownerWindow.mockReturnValue(window);
178+
179+
return Effect.gen(function* () {
180+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
181+
assert.equal(paste.mock.calls.length, 1);
182+
assert.equal(mainPaste.mock.calls.length, 0);
183+
184+
yield* pasteAsText.handler(undefined, { sender: { id: 99 } });
185+
assert.equal(paste.mock.calls.length, 1);
186+
ownerWindow.mockReturnValue({}); // A focused PiP/other BrowserWindow.
187+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
188+
assert.equal(paste.mock.calls.length, 1);
189+
ownerWindow.mockReturnValue(null); // Detached contents.
190+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
191+
assert.equal(paste.mock.calls.length, 1);
192+
ownerWindow.mockReturnValue(window);
193+
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => true });
194+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
195+
assert.equal(paste.mock.calls.length, 1);
196+
focusedWebContents.mockReturnValue(null);
197+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
198+
assert.equal(paste.mock.calls.length, 1);
199+
}).pipe(
200+
Effect.provide(
201+
Layer.mock(ElectronWindow.ElectronWindow)({
202+
main: Effect.succeed(Option.some(window)),
203+
}),
204+
),
205+
);
206+
},
207+
);
208+
});
209+
156210
describe("pickProjectFavicon", () => {
157211
it.effect("opens a single-image picker from the project directory", () =>
158212
Effect.gen(function* () {

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,32 @@ export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
345345
}),
346346
});
347347

348+
export const pasteAsText = DesktopIpc.makeIpcMethod({
349+
channel: IpcChannels.PASTE_AS_TEXT_CHANNEL,
350+
payload: Schema.Undefined,
351+
result: Schema.Void,
352+
handler: Effect.fn("desktop.ipc.window.pasteAsText")(function* (_input, event) {
353+
const electronWindow = yield* ElectronWindow.ElectronWindow;
354+
const window = yield* electronWindow.main;
355+
if (
356+
event === undefined ||
357+
Option.isNone(window) ||
358+
window.value.isDestroyed() ||
359+
window.value.webContents.id !== event.sender.id
360+
) {
361+
return;
362+
}
363+
const focused = Electron.webContents.getFocusedWebContents();
364+
if (
365+
focused &&
366+
!focused.isDestroyed() &&
367+
Electron.BrowserWindow.fromWebContents(focused) === window.value
368+
) {
369+
focused.paste();
370+
}
371+
}),
372+
});
373+
348374
/** Theme files are a few KB; anything larger returns empty text and lets the
349375
* renderer reject it by size without the contents ever crossing the bridge. */
350376
const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
157157
openSystemSettings: (pane: string) =>
158158
ipcRenderer.invoke(IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL, pane),
159159
probeRemoteEditors: () => ipcRenderer.invoke(IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, undefined),
160+
pasteAsText: () => ipcRenderer.invoke(IpcChannels.PASTE_AS_TEXT_CHANNEL, undefined),
160161
onMenuAction: (listener) => {
161162
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
162163
if (typeof action !== "string") return;

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,36 @@ describe("DesktopApplicationMenu", () => {
153153
}),
154154
);
155155

156+
it.effect("owns Paste as Text and routes it through the renderer", () =>
157+
Effect.gen(function* () {
158+
const selectedAction = yield* Deferred.make<string>();
159+
const applicationMenuTemplate =
160+
yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
161+
162+
yield* configureMenu(selectedAction, applicationMenuTemplate);
163+
164+
const template = yield* Deferred.await(applicationMenuTemplate);
165+
const editMenu = template.find((item) => item.label === "Edit");
166+
assert.isDefined(editMenu);
167+
if (!Array.isArray(editMenu.submenu)) {
168+
throw new Error("Expected Edit menu submenu to be an array.");
169+
}
170+
const pasteAsTextItem = editMenu.submenu.find((item) => item.label === "Paste as Text");
171+
assert.isDefined(pasteAsTextItem);
172+
assert.equal(pasteAsTextItem.accelerator, "CmdOrCtrl+Shift+V");
173+
if (typeof pasteAsTextItem.click !== "function") {
174+
throw new Error("Expected Paste as Text menu item to have a click handler.");
175+
}
176+
177+
pasteAsTextItem.click(
178+
{} as Electron.MenuItem,
179+
{} as Electron.BrowserWindow,
180+
{} as KeyboardEvent,
181+
);
182+
assert.equal(yield* Deferred.await(selectedAction), "paste-as-text");
183+
}),
184+
);
185+
156186
// Zoom must route through DesktopWindow.zoomMain instead of the Electron
157187
// zoom roles: the roles zoom whichever webContents has focus, which breaks
158188
// app zoom while an embedded preview WebContentsView holds focus.

‎apps/desktop/src/window/DesktopApplicationMenu.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ const dispatchMenuAction = Effect.fn("desktop.menu.dispatchMenuAction")(function
4646
action: string,
4747
): Effect.fn.Return<void, DesktopWindow.DesktopWindowError, DesktopWindow.DesktopWindow> {
4848
const desktopWindow = yield* DesktopWindow.DesktopWindow;
49-
yield* desktopWindow.dispatchMenuAction(action);
49+
yield* desktopWindow.dispatchMenuAction(action, {
50+
reveal: action !== "paste-as-text",
51+
});
5052
});
5153

5254
const zoomMainWindow = Effect.fn("desktop.menu.zoomMainWindow")(function* (
@@ -135,6 +137,9 @@ export const make = Effect.gen(function* () {
135137
const settingsClick = () => {
136138
runMenuEffect("open-settings", dispatchMenuAction("open-settings"));
137139
};
140+
const pasteAsTextClick = () => {
141+
runMenuEffect("paste-as-text", dispatchMenuAction("paste-as-text"));
142+
};
138143
const zoomClick = (direction: DesktopWindow.MainWindowZoomDirection) => () => {
139144
runMenuEffect(`zoom-${direction}`, zoomMainWindow(direction));
140145
};
@@ -184,7 +189,34 @@ export const make = Effect.gen(function* () {
184189
{ role: environment.platform === "darwin" ? "close" : "quit" },
185190
],
186191
},
187-
{ role: "editMenu" },
192+
{
193+
label: "Edit",
194+
submenu: [
195+
{ role: "undo" },
196+
{ role: "redo" },
197+
{ type: "separator" },
198+
{ role: "cut" },
199+
{ role: "copy" },
200+
{ role: "paste" },
201+
{
202+
label: "Paste as Text",
203+
accelerator: "CmdOrCtrl+Shift+V",
204+
click: pasteAsTextClick,
205+
},
206+
{ role: "delete" },
207+
{ type: "separator" },
208+
{ role: "selectAll" },
209+
...(environment.platform === "darwin"
210+
? [
211+
{ type: "separator" as const },
212+
{
213+
label: "Speech",
214+
submenu: [{ role: "startSpeaking" as const }, { role: "stopSpeaking" as const }],
215+
},
216+
]
217+
: []),
218+
],
219+
},
188220
{
189221
label: "View",
190222
submenu: [

‎apps/mobile/modules/t3-composer-editor/android/build.gradle‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ android {
88
namespace 'expo.modules.t3composereditor'
99
compileSdk rootProject.ext.compileSdkVersion
1010

11+
testOptions {
12+
unitTests.includeAndroidResources = true
13+
}
14+
1115
defaultConfig {
1216
minSdkVersion rootProject.ext.minSdkVersion
1317
targetSdkVersion rootProject.ext.targetSdkVersion
@@ -17,4 +21,12 @@ android {
1721
dependencies {
1822
implementation project(':expo-modules-core')
1923
implementation project(':t3tools-mobile-markdown-text')
24+
testImplementation 'junit:junit:4.13.2'
25+
testImplementation 'org.robolectric:robolectric:4.16.1'
26+
}
27+
28+
tasks.withType(Test).configureEach {
29+
javaLauncher = javaToolchains.launcherFor {
30+
languageVersion = JavaLanguageVersion.of(21)
31+
}
2032
}

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorModule.kt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ class T3ComposerEditorModule : Module() {
116116
Prop("spellCheck") { view: T3ComposerEditorView, spellCheck: Boolean ->
117117
view.setSpellCheck(spellCheck)
118118
}
119+
Prop("textPasteThresholdBytes") { view: T3ComposerEditorView, threshold: Int ->
120+
view.setTextPasteThresholdBytes(threshold)
121+
}
122+
Prop("maxInputChars") { view: T3ComposerEditorView, maxInputChars: Int ->
123+
view.setMaxInputChars(maxInputChars)
124+
}
119125

120126
Events(
121127
"onComposerChange",
@@ -125,6 +131,7 @@ class T3ComposerEditorModule : Module() {
125131
"onComposerPasteImages",
126132
"onComposerContextPress",
127133
"onComposerPasteContext",
134+
"onComposerPasteText",
128135
"onComposerContentSizeChange",
129136
)
130137

0 commit comments

Comments
 (0)