@atlaskit/editor-plugin-engagement-platform
Version:
Engagement platform plugin for @atlaskit/editor-core“
86 lines • 2.75 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { engagementPlatformPmPluginKey } from './engagementPlatformPmPluginKey';
export const engagementPlatformPmPlugin = pluginConfig => {
return new SafePlugin({
key: engagementPlatformPmPluginKey,
state: {
init: () => {
return {
messageStates: {},
startMessagePromises: {},
stopMessagePromises: {},
epComponents: pluginConfig.epComponents,
epHooks: pluginConfig.epHooks,
coordinationClient: pluginConfig.coordinationClient
};
},
apply: (tr, pluginState) => {
const meta = tr.getMeta(engagementPlatformPmPluginKey);
if (!meta) {
return pluginState;
}
let state = {
...pluginState
};
for (const command of meta.commands) {
switch (command.type) {
case 'setMessageState':
state = {
...state,
messageStates: {
...state.messageStates,
[command.messageId]: command.state
}
};
break;
case 'setStartMessagePromise':
if (command.promise !== undefined) {
state = {
...state,
startMessagePromises: {
...state.startMessagePromises,
[command.messageId]: command.promise
}
};
break;
} else {
// If the promise is undefined, remove it from the state
const {
[command.messageId]: _,
...startMessagePromises
} = state.startMessagePromises;
state = {
...state,
startMessagePromises
};
}
break;
case 'setStopMessagePromise':
if (command.promise !== undefined) {
state = {
...state,
stopMessagePromises: {
...state.stopMessagePromises,
[command.messageId]: command.promise
}
};
break;
} else {
// If the promise is undefined, remove it from the state
const {
[command.messageId]: _,
...stopMessagePromises
} = state.stopMessagePromises;
state = {
...state,
stopMessagePromises
};
}
break;
}
}
return state;
}
}
});
};