feat(share): remove electron share again

This commit is contained in:
Johannes Millan 2025-10-22 16:05:29 +02:00
parent 459b189e26
commit c58e269678
2 changed files with 6 additions and 160 deletions

View file

@ -22,106 +22,6 @@ import { quitApp, showOrFocus } from './various-shared';
import { loadSimpleStoreAll, saveSimpleStore } from './simple-store';
import { BACKUP_DIR, BACKUP_DIR_WINSTORE } from './backup';
import { pluginNodeExecutor } from './plugin-node-executor';
import { clipboard } from 'electron';
interface SharePayload {
text?: string;
url?: string;
title?: string;
}
/**
* Handle share on macOS using AppleScript to invoke system share dialog.
* Falls back to clipboard if AppleScript fails.
*/
const handleMacOSShare = async (
payload: SharePayload,
): Promise<{
success: boolean;
error?: string;
}> => {
const { text, url, title } = payload;
const contentToShare = [title, text, url].filter(Boolean).join('\n\n');
if (!contentToShare) {
return { success: false, error: 'No content to share' };
}
return new Promise((resolve) => {
// Use AppleScript to trigger native share
// This creates a share menu at the mouse cursor position
const appleScript = `
tell application "System Events"
set the clipboard to "${contentToShare.replace(/"/g, '\\"').replace(/\n/g, '\\n')}"
end tell
display dialog "Content copied to clipboard. Use Cmd+V to paste in your desired app." buttons {"OK"} default button "OK" with icon note
`;
exec(`osascript -e '${appleScript.replace(/'/g, "'\\''")}'`, (error) => {
if (error) {
log('AppleScript share failed, falling back to clipboard:', error);
// Fallback: just copy to clipboard
try {
clipboard.writeText(contentToShare);
resolve({ success: true });
} catch (clipboardError) {
resolve({
success: false,
error: 'Failed to copy to clipboard',
});
}
} else {
clipboard.writeText(contentToShare);
resolve({ success: true });
}
});
});
};
/**
* Handle share on Windows using clipboard.
* Note: Proper Windows Share UI requires UWP/WinRT APIs which need native modules.
* This implementation copies to clipboard as a practical fallback.
*/
const handleWindowsShare = async (
payload: SharePayload,
): Promise<{
success: boolean;
error?: string;
}> => {
const { text, url, title } = payload;
const contentToShare = [title, text, url].filter(Boolean).join('\n\n');
if (!contentToShare) {
return { success: false, error: 'No content to share' };
}
try {
// Copy to clipboard
clipboard.writeText(contentToShare);
// Show notification dialog
const mainWin = getWin();
if (mainWin) {
await dialog.showMessageBox(mainWin, {
type: 'info',
title: 'Content Copied',
message: 'Content has been copied to clipboard',
detail: 'You can now paste it in any application using Ctrl+V',
buttons: ['OK'],
});
}
return { success: true };
} catch (error) {
log('Windows share failed:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to copy to clipboard',
};
}
};
export const initIpcInterfaces = (): void => {
// Initialize plugin node executor (registers IPC handlers)
@ -179,35 +79,10 @@ export const initIpcInterfaces = (): void => {
return { success: false };
});
ipcMain.handle(IPC.SHARE_NATIVE, async (ev, payload) => {
const { text, url, title } = payload;
const platform = process.platform;
try {
// macOS: Use system share via AppleScript
if (platform === 'darwin') {
return await handleMacOSShare({ text, url, title });
}
// Windows: Use clipboard + notification as fallback
// Note: Proper Windows Share UI requires UWP/WinRT which needs native module
if (platform === 'win32') {
return await handleWindowsShare({ text, url, title });
}
// Linux: No native share available
log('Linux platform - no native share available, using fallback');
return {
success: false,
error: 'Native share not available on Linux',
};
} catch (error) {
log('Native share error:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Share failed',
};
}
ipcMain.handle(IPC.SHARE_NATIVE, async () => {
// Desktop platforms use the share dialog instead of native share
// This allows for more flexibility and better UX with social media options
return { success: false, error: 'Native share not available on desktop' };
});
ipcMain.on(IPC.LOCK_SCREEN, () => {

View file

@ -1,7 +1,6 @@
import { Injectable, inject } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Capacitor } from '@capacitor/core';
import { IS_ELECTRON } from '../../app.constants';
import { IS_ANDROID_WEB_VIEW } from '../../util/is-android-web-view';
import { SnackService } from '../snack/snack.service';
import { SharePayload, ShareResult, ShareTarget, ShareTargetConfig } from './share.model';
@ -113,26 +112,10 @@ export class ShareService {
}
/**
* Try to use native share (Electron, Android, Web Share API).
* Try to use native share (Android, Web Share API).
* Public API for dialog component.
*/
async tryNativeShare(payload: SharePayload): Promise<ShareResult> {
if (IS_ELECTRON && typeof window.ea?.shareNative === 'function') {
try {
const result = await window.ea.shareNative(payload);
if (result.success) {
this._snackService.open('Shared successfully!');
return {
success: true,
usedNative: true,
target: 'native',
};
}
} catch (error) {
console.warn('Electron native share failed:', error);
}
}
const capacitorShare = await this._getCapacitorSharePlugin();
if (capacitorShare) {
try {
@ -254,11 +237,7 @@ export class ShareService {
): Promise<ShareResult> {
const url = this._buildShareUrl(payload, target);
if (IS_ELECTRON && window.ea?.openExternalUrl) {
window.ea.openExternalUrl(url);
} else {
window.open(url, '_blank', 'noopener,noreferrer');
}
window.open(url, '_blank', 'noopener,noreferrer');
this._snackService.open('Opening share window...');
@ -367,10 +346,6 @@ export class ShareService {
* Check if native/system share is available on current platform.
*/
private async _isSystemShareAvailable(): Promise<boolean> {
if (IS_ELECTRON && typeof window.ea?.shareNative === 'function') {
return true;
}
if (await this._isCapacitorShareAvailable()) {
return true;
}
@ -443,10 +418,6 @@ export class ShareService {
}
private async _detectShareSupport(): Promise<ShareSupport> {
if (IS_ELECTRON && typeof window.ea?.shareNative === 'function') {
return 'native';
}
if (await this._isCapacitorShareAvailable()) {
return 'native';
}