Skip to content

Commit 10d30d0

Browse files
jardiccJaroslav
and
Jaroslav
authored
Adds option to convert _path into tokens by default (#44)
* adds option to convert _path into tokens by default * Makes it compatible with MacOS and Windows --------- Co-authored-by: Jaroslav <[email protected]>
1 parent 4d971ce commit 10d30d0

File tree

10 files changed

+64
-24
lines changed

10 files changed

+64
-24
lines changed

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"typescript.tsdk": "node_modules\\typescript\\lib"
2+
"typescript.tsdk": "node_modules\\typescript\\lib",
3+
"cSpell.words": [
4+
"tokenify"
5+
]
36
}

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.7.0
4+
5+
- adds option to convert `_path` into tokens by default in generated code
6+
37
## 2.6.0
48

59
- adds icons to the recorded descriptors in descriptors list

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 5,
33
"name": "Alchemist",
44
"id": "2bcdb900",
5-
"version": "2.6.0",
5+
"version": "2.7.0",
66
"host": {
77
"app": "PS",
88
"minVersion": "24.2",

installer/2bcdb900_PS.ccx

299 Bytes
Binary file not shown.

src/inspector/components/Settings/SettingsContainer.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Settings extends Component<TSettings, ISettingsState> {
130130
const {settings: {makeRawDataEasyToInspect: ignoreRawData, maximumItems, fontSize, neverRecordActionNames, accordionExpandedIDs}, onSetRecordRaw, onSetFontSize, onNeverRecordActionNamesChanged} = this.props;
131131
const {onSetGlobalOptions, settingsVisible: visible, setToggleSettings, onToggleAccordion} = this.props;
132132
const {dialogOptions, modalBehavior, synchronousExecution, supportRawDataType} = this.props.descriptorSettings;
133-
const {indent, singleQuotes, hideDontRecord, hideForceNotify, hide_isCommand,codeImports,codeWrappers} = this.props.globalSettings;
133+
const {indent, singleQuotes, hideDontRecord, hideForceNotify, hide_isCommand,codeImports,codeWrappers,tokenify} = this.props.globalSettings;
134134

135135
const items: {val: TFontSizeSettings, label: string}[] = [
136136
{label: "Tiny", val: "size-tiny"},
@@ -241,6 +241,12 @@ class Settings extends Component<TSettings, ISettingsState> {
241241
checked={singleQuotes || undefined}
242242
>Use single quotes</SP.Checkbox>
243243
</div>
244+
<div className="row">
245+
<SP.Checkbox
246+
onChange={(e) => onSetGlobalOptions({tokenify: !!e.target?.checked})}
247+
checked={tokenify || undefined}
248+
>Convert _path to tokens</SP.Checkbox>
249+
</div>
244250

245251
<div className="row">
246252
<SP.Checkbox

src/inspector/inspInitialState.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getSorInitialState } from "../sorcerer/sorInitialState";
77

88
export function getInitialState(): IInspectorState {
99
return {
10-
version: [12, 0, 0],
10+
version: [13, 0, 0],
1111
selectedReferenceType: "layer",
1212
filterBySelectedReferenceType: "off",
1313
descriptorsGrouping: "eventName",
@@ -183,6 +183,7 @@ export function getInitialState(): IInspectorState {
183183
},
184184
},
185185
settings: {
186+
186187
settingsVisible:false,
187188
/** Sometimes you can get data when object in reference array is selected. This option is intended to select that item automatically for you */
188189
fontSize: "size-default",
@@ -700,6 +701,7 @@ export function getInitialState(): IInspectorState {
700701
singleQuotes: false,
701702
codeImports: "require",
702703
codeWrappers: "modal",
704+
tokenify: true,
703705

704706
hide_isCommand: true,
705707
hideDontRecord: true,

src/inspector/model/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export interface ISettings {
297297
neverRecordActionNames: string[]
298298
accordionExpandedIDs: string[]
299299

300+
tokenify:boolean
300301
singleQuotes: boolean
301302
indent: "tab" | "space1" | "space2" | "space3" | "space4" | "space5" | "space6" | "space7" | "space8"
302303

src/inspector/selectors/inspectorCodeSelectors.ts

+42-18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export const getGeneratedCode = createSelector([
5353
getReplayEnabled, getDescriptorOptions, getInspectorSettings, getIndentString,
5454
], (selected, autoActive, treePath, replayEnabled, descOptions, settings, tab) => {
5555

56+
let shouldShowTokenify = false;
57+
5658
function makeNicePropertyPath(segments: string[]): string {
5759
const regex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/m;
5860

@@ -93,27 +95,27 @@ export const getGeneratedCode = createSelector([
9395

9496
const res: BatchPlayCommandOptionsExtended = {};
9597

96-
if (hasAnySync) { res.synchronousExecution = true; }
97-
else if (hasAnyAsync) { res.synchronousExecution = false; }
98+
if (hasAnySync) {res.synchronousExecution = true;}
99+
else if (hasAnyAsync) {res.synchronousExecution = false;}
98100

99-
if (modalIsExecute) { res.modalBehavior = "execute"; }
100-
else if (modalIsWait) { res.modalBehavior = "wait"; }
101-
else if (!modalIsDefault) { res.modalBehavior = "fail"; }
101+
if (modalIsExecute) {res.modalBehavior = "execute";}
102+
else if (modalIsWait) {res.modalBehavior = "wait";}
103+
else if (!modalIsDefault) {res.modalBehavior = "fail";}
102104

103105
return res;
104106
}
105107

106108
// adds indentation
107-
function idt(str:string):string {
109+
function idt(str: string): string {
108110
return str.split("\n").map(l => tab + l).join("\n");
109111
}
110112

111-
function udt(str: string): string{
113+
function udt(str: string): string {
112114
return str.split("\n").map(l => l.replace(tab, "")).join("\n");
113115
}
114116

115117
// replaces quotes
116-
function qts(str:string):string {
118+
function qts(str: string): string {
117119
if (settings.singleQuotes) {
118120
str = str.replaceAll(`"`, `'`);
119121
}
@@ -124,13 +126,23 @@ export const getGeneratedCode = createSelector([
124126
const wrappers = settings.codeWrappers;
125127

126128
if (selected.length >= 1 || autoActive) {
127-
let data:any = null, iDesc: IDescriptor[] = [];
129+
let data: any = null, iDesc: IDescriptor[] = [];
128130

129131
const stringifyOptions = {
130132
singleQuotes: settings.singleQuotes,
131133
indent: tab,
134+
transform: (input: any | object, prop: number | string | symbol, originalResult: string) => {
135+
if (settings.tokenify && prop === "_path") {
136+
shouldShowTokenify = true;
137+
console.log(input, originalResult);
138+
const res = qts(`await tokenify("${input._path.replaceAll("\\","/")}")`);
139+
return res;
140+
}
141+
return originalResult;
142+
},
132143
};
133144

145+
const tokenifyInfo = "// Please make sure that file system access permission in manifest.json has correct value.\n\n";
134146
const playAbleInfo = "// Alchemist can't generate code from the reply of replay and from dispatched code.";
135147
const notifierWarning = "// Events recognized as notifiers are not re-playable in most of the cases. There is high chance that generated code won't work.\n\n";
136148

@@ -139,7 +151,7 @@ export const getGeneratedCode = createSelector([
139151
} else if (autoActive) {
140152
iDesc = [autoActive];
141153
}
142-
if (iDesc.some(item => ["replies","dispatcher"].includes(item.originalReference.type))) {
154+
if (iDesc.some(item => ["replies", "dispatcher"].includes(item.originalReference.type))) {
143155
return playAbleInfo;
144156
}
145157
data = iDesc.map(item => addPerItemOptions(item));
@@ -149,20 +161,20 @@ export const getGeneratedCode = createSelector([
149161
if (Array.isArray(data)) {
150162
data.forEach(d => {
151163
if (settings.hideDontRecord) {
152-
delete d.dontRecord;
164+
delete d.dontRecord;
153165
}
154166
if (settings.hideForceNotify) {
155-
delete d.forceNotify;
167+
delete d.forceNotify;
156168
}
157169
if (settings.hide_isCommand) {
158170
delete d._isCommand;
159-
}
171+
}
160172
});
161173
}
162174

163175
for (let i = 0; i < data.length; i++) {
164176
const item = data[i];
165-
RawDataConverter.convertFakeRawInCode(item,descOptions);
177+
RawDataConverter.convertFakeRawInCode(item, descOptions);
166178
}
167179

168180
let strPinned = "";
@@ -175,34 +187,46 @@ export const getGeneratedCode = createSelector([
175187
const commandOptions = addCommonOptions(iDesc);
176188

177189
const strOptions = idt(stringifyObject(commandOptions, stringifyOptions));
178-
const strDesc:string = stringifyObject(data, stringifyOptions);
190+
const strDesc: string = stringifyObject(data, stringifyOptions);
179191

180192

181193
const strExecModalImport = addModules ? qts(`const {executeAsModal} = require("photoshop").core;\n`) : "";
182-
const strBatchPlayImport = addModules ? qts(`const {batchPlay} = require("photoshop").action;\n\n`) : "";
194+
const strBatchPlayImport = addModules ? qts(`const {batchPlay} = require("photoshop").action;\n`) : "";
195+
const strTokenifyImport = addModules && shouldShowTokenify ? qts(`const {localFileSystem: fs} = require("uxp").storage;\n`) : "";
196+
const strNewLine = addModules ? "\n" : "";
183197

184198
const strBatchPlay = `const result = await batchPlay(\n${idt(strDesc)},\n${strOptions}\n);${strPinned}`;
185199
const strActionCommand = `async function actionCommands() {\n${idt(strBatchPlay)}\n}\n\n`;
186200

187201
const strCall = qts(`async function runModalFunction() {\n${tab}await executeAsModal(actionCommands, {"commandName": "Action Commands"});\n}\n\nawait runModalFunction();\n`);
202+
const strTokenify = shouldShowTokenify ? qts(`async function tokenify(url){\n${tab}return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));\n}\n\n`) : "";
188203

189204
let banner = "";
190205
if (iDesc.some(item => item.originalReference.type === "notifier")) {
191206
banner = notifierWarning;
192207
}
208+
if (shouldShowTokenify) {
209+
banner += tokenifyInfo;
210+
}
193211

194212

195-
if (wrappers==="batchPlay") {
213+
if (wrappers === "batchPlay") {
196214
return (
197215
banner +
198216
strBatchPlayImport +
217+
strTokenifyImport +
218+
strNewLine +
219+
strTokenify +
199220
strBatchPlay
200221
);
201-
} else if (wrappers==="modal") {
222+
} else if (wrappers === "modal") {
202223
return (
203224
banner +
204225
strExecModalImport +
205226
strBatchPlayImport +
227+
strTokenifyImport +
228+
strNewLine +
229+
strTokenify +
206230
strActionCommand +
207231
strCall
208232
);

uxp/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 5,
33
"name": "Alchemist",
44
"id": "2bcdb900",
5-
"version": "2.6.0",
5+
"version": "2.7.0",
66
"host": {
77
"app": "PS",
88
"minVersion": "24.2",

0 commit comments

Comments
 (0)