forked from wulkano/Kap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-options.js
104 lines (84 loc) · 2.61 KB
/
export-options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Store = require('electron-store');
const {ipcMain: ipc} = require('electron-better-ipc');
const plugins = require('./common/plugins');
const {converters} = require('./convert');
const {setOptions, getEditors} = require('./editor');
const {apps} = require('./plugins/open-with-plugin');
const exportUsageHistory = new Store({
name: 'export-usage-history',
defaults: {
apng: {lastUsed: 1, plugins: {default: 1}},
webm: {lastUsed: 2, plugins: {default: 1}},
mp4: {lastUsed: 3, plugins: {default: 1}},
gif: {lastUsed: 4, plugins: {default: 1}}
}
});
const prettifyFormat = format => {
const formats = new Map([
['apng', 'APNG'],
['gif', 'GIF'],
['mp4', 'MP4'],
['webm', 'WebM']
]);
return formats.get(format);
};
const getExportOptions = () => {
const installed = plugins.getInstalled();
const builtIn = plugins.getBuiltIn();
const options = [];
for (const format of converters.keys()) {
options.push({
format,
prettyFormat: prettifyFormat(format),
plugins: []
});
}
for (const json of [...installed, ...builtIn]) {
if (!json.isCompatible) {
continue;
}
const plugin = require(json.pluginPath);
for (const service of plugin.shareServices) {
for (const format of service.formats) {
options.find(option => option.format === format).plugins.push({
title: service.title,
pluginName: json.name,
pluginPath: json.pluginPath,
apps: json.name === '_openWith' ? apps.get(format) : undefined
});
}
}
}
const sortFunc = (a, b) => b.lastUsed - a.lastUsed;
for (const option of options) {
const {lastUsed, plugins} = exportUsageHistory.get(option.format);
option.lastUsed = lastUsed;
option.plugins = option.plugins.map(plugin => ({...plugin, lastUsed: plugins[plugin.pluginName] || 0})).sort(sortFunc);
}
return options.sort(sortFunc);
};
const updateExportOptions = () => {
const editors = getEditors();
const exportOptions = getExportOptions();
for (const editor of editors) {
ipc.callRenderer(editor, 'export-options', exportOptions);
}
setOptions(exportOptions);
};
plugins.setUpdateExportOptions(updateExportOptions);
ipc.answerRenderer('update-usage', ({format, plugin}) => {
const usage = exportUsageHistory.get(format);
const now = Date.now();
usage.plugins[plugin] = now;
usage.lastUsed = now;
exportUsageHistory.set(format, usage);
updateExportOptions();
});
const initializeExportOptions = () => {
setOptions(getExportOptions());
};
module.exports = {
getExportOptions,
updateExportOptions,
initializeExportOptions
};