Skip to content

Commit

Permalink
added TargetApp support
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolba committed Mar 29, 2022
1 parent dfc4969 commit 1d80712
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 59 deletions.
72 changes: 69 additions & 3 deletions packages/main/src/listeners/fdc3Listeners.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Listener as IListener } from '../types/Listener';
import { Context, AppIntent, AppMetadata, IntentMetadata } from '@finos/fdc3';
import { Context, AppIntent, AppMetadata, IntentMetadata, TargetApp } from '@finos/fdc3';
import { FDC3Message } from '../types/FDC3Message';
import { Channel, DirectoryApp, FDC3App } from '../types/FDC3Data';
import utils from '../utils';
Expand Down Expand Up @@ -257,13 +257,77 @@ interface ViewListener {
listenerId: string;
}


/**
*
* @param target
* Given a TargetApp input, return the app Name or undefined
*/
const resolveTargetAppToName = (target : TargetApp) : string | undefined => {
if (!target){
return undefined;
} else {
let name = undefined;
//is target typeof string? if so, it is just going to be an app name
if (typeof(target) === 'string'){
name = target;
}
else {
const app : AppMetadata = (target as AppMetadata);
if (app && app.name) {
name = app.name;
}
}
return name;
}
};

/**
*
* @param target
* Given a TargetApp input, return a search query string to append to an appD search call
* e.g. '&name=AppName' or '&text=AppTitle'
*/
const resolveTargetAppToQuery = (target : TargetApp) : string => {
if (!target){
return '';
} else {
let query = '';
//is there a valid app name?
const name = resolveTargetAppToName(target);
if (name) {
query = `&name=${name}`;
}
else {
const app : AppMetadata = (target as AppMetadata);
if (app) {
//construct a text search, prefering id, then title, then description
//this is currently punting on a more complicated heuristic on potentailly ambiguous results (by version, etc)
if (app.appId) {
query = `&text=${app.appId}`;
}
else if (app.title) {
query = `&text=${app.title}`;
}
else if (app.description) {
query = `&text=${app.description}`;
}
}
}
return query;
}
};


_listeners.push({
name: TOPICS.FDC3_OPEN,
handler: (runtime, msg) => {
return new Promise((resolve, reject) => {
console.log('fdc3Message recieved', msg);

runtime.fetchFromDirectory(`/apps/${msg.data.name}`).then(
const name = resolveTargetAppToName(msg.data.target) || '';

runtime.fetchFromDirectory(`/apps/${name}`).then(
(result: DirectoryApp) => {
// const source = utils.id(port);
if (result) {
Expand Down Expand Up @@ -900,10 +964,12 @@ _listeners.push({
ctx = msg.data.context.type;
}
utils.getDirectoryUrl().then(async (directoryUrl) => {
const query = resolveTargetAppToQuery(msg.data.target);

const _r = await fetch(
`${directoryUrl}/apps/search?intent=${
msg.data.intent
}&context=${ctx}&name=${msg.data.target ? msg.data.target : ''}`,
}&context=${ctx}${query}`,
);
console.log('raiseIntent', _r);
if (_r) {
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/types/FDC3Event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from '@finos/fdc3';
import { Context, TargetApp } from '@finos/fdc3';

/**
* Custom DOM event used by the FDC3 API
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface FDC3EventDetail {
data?: any;
name?: string;
context?: Context;
target?: string;
target?: TargetApp;
source?: string;
/* identifier of the browserView the event originated from */
viewId?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/preload/exposedInMainWorld.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ interface Window {
readonly versions: NodeJS.ProcessVersions;
readonly workspace: { isConnected: () => boolean; };
readonly home: { getApps: () => Promise<unknown>; };
readonly fdc3: { getInfo(): import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (name: string, context?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; broadcast: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, target: string) => Promise<any>; addContextListener: (contextType: any, handler?: any) => Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; getSystemChannels: () => Promise<any>; getOrCreateChannel: (channelId: string) => Promise<any>; joinChannel: (channel: string) => Promise<void>; leaveCurrentChannel: () => Promise<any>; getCurrentChannel: () => Promise<any>; getAppInstance: (instanceId: string) => Promise<import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/packages/main/src/types/AppInstance").AppInstance>; };
readonly fdc3: { getInfo(): import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (app: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp, context?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; broadcast: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp) => Promise<any>; raiseIntentForContext(context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp): Promise<any>; addContextListener: (contextType: any, handler?: any) => Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; getSystemChannels: () => Promise<any>; getOrCreateChannel: (channelId: string) => Promise<any>; joinChannel: (channel: string) => Promise<void>; leaveCurrentChannel: () => Promise<any>; getCurrentChannel: () => Promise<any>; getAppInstance: (instanceId: string) => Promise<import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/packages/main/src/types/AppInstance").AppInstance>; };
}
62 changes: 9 additions & 53 deletions packages/preload/src/view/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ContextHandler,
Channel,
ImplementationMetadata,
TargetApp
} from '@finos/fdc3';
import { FDC3Event, FDC3EventDetail } from '../../../main/src/types/FDC3Event';
import { FDC3EventEnum } from '../../../main/src/types/FDC3Event';
Expand Down Expand Up @@ -216,23 +217,27 @@ export function createAPI() {
};
},

open: (name: string, context?: Context) => {
return wireMethod('open', { name: name, context: context });
open: (app: TargetApp, context?: Context) => {
return wireMethod('open', { target: app, context: context });
},

broadcast: (context: Context) => {
//void
wireMethod('broadcast', { context: context }, { void: true });
},

raiseIntent: (intent: string, context: Context, target: string) => {
raiseIntent: (intent: string, context: Context, app?: TargetApp) => {
return wireMethod('raiseIntent', {
intent: intent,
context: context,
target: target,
target: app,
});
},

raiseIntentForContext(context : Context, app? : TargetApp) {
return wireMethod("raiseIntentForContext",{context:context, target: app});
},

addContextListener: (contextType: any, handler?: any) => {
const thisListener: ContextHandler = handler ? handler : contextType;
const thisContextType: string =
Expand Down Expand Up @@ -386,55 +391,6 @@ export function createAPI() {
);
});

(document as any).addEventListener(TOPICS.NAVIGATE, (event: CustomEvent) => {
if (event.detail.href) {
window.location.href = event.detail.href;
}
});

window.addEventListener('contextmenu', (e: MouseEvent) => {
e.preventDefault();
const detail: any = {};
//get target
/*
standard datapoints:
document href
document title
app name
manifest details: img, etc
if anchor
link href
inner text
title (if any)
title of contents (if image)
if image
src
title
find topic
append all inner text where that topic appears, filter out non-topical words...
*/
const target: HTMLElement = e.target as HTMLElement;
const tagName = target.tagName;
detail.tagName = tagName;
detail.source = document.location.href;
detail.title = document.title;
if (target.textContent) {
detail.text = target.textContent;
}

//taget text content
//is it an image?
//what else is in the context?
document.dispatchEvent(
new CustomEvent(TOPICS.CONTEXT_MENU, {
detail: detail,
}),
);
});
//map of context listeners by id
const _contextListeners: Map<string, ListenerItem> = new Map();

Expand Down

0 comments on commit 1d80712

Please sign in to comment.