-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMenuCamelCase.jsx
More file actions
147 lines (125 loc) · 4.52 KB
/
MenuCamelCase.jsx
File metadata and controls
147 lines (125 loc) · 4.52 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// =============================================================================
// MenuCamelCase [210328] [210402]
// Simple implementation of Type > Change Case > CamelCase
// ---
// The targetengine directive can be commented for testing/debugging.
// It is not mandatory in this particular example because the action
// event handler is the present File itself -- which is persistent!
// However, adding a persistent engine when deploying the script is
// useful, and recommended, to prevent the handler from entering the
// `onEngine()` block again and again, which makes it much faster.
// =============================================================================
#targetengine 'CamelCase'
#include '../$$.jsxinc'
#include '../etc/$$.Dom.Menu.jsxlib'
;$$.hasOwnProperty('CamelCase') || eval(__(MODULE, $$, 'CamelCase', 210402, 'onRunning'))
[PRIVATE]
({
CAML: function( tx,a,i)
//----------------------------------
// Implements the CamelCase routine.
{
if( !(tx=callee.TEXT) ) return;
a = tx.contents.split(/[ _-]+/g);
for( i=a.length ; i-- ; a[i]=a[i].charAt(0).toUpperCase() + a[i].slice(1).toLowerCase() );
tx.contents = a.join('');
}
.setup
({
TEXT: false, // expecting a Text object.
}),
})
[PUBLIC]
({
UniqueLabel: 'MyCamelCaseUID',
OkAction: false,
onEngine: function onEngine_( refs,m,ok,msg)
//----------------------------------
{
refs = [];
// Main > Type > Change Case > <lastMenuItem>
( m = $$.Dom.Menu.getMenuItem([0,'','$ID/CCase Cmd', -1]) )
? $$.trace(__("%1 > Primary reference menu successfully identified.", callee.µ))
: $$.warn( __("%1 > Cannot identify the primary reference menu.", callee.µ));
m && refs.push({ ref:m, pos:'after' });
// RightClickText > Change Case > <lastMenuItem>
( m = $$.Dom.Menu.getMenuItem(['$ID/RtMouseText', '$ID/CCase Cmd', -1]) )
? $$.trace(__("%1 > Contextual menu successfully identified.", callee.µ))
: $$.warn( __("%1 > Cannot identify the contextual menu.", callee.µ));
m && refs.push({ ref:m, pos:'after' });
ok = refs.length && $$.Dom.Menu.setAction
(
__("CamelCase"), // title (could be localized thru Yalt)
refs, // more menu refs could be added to this array
{
events: 'beforeDisplay_onInvoke', // managed events
label: this.UniqueLabel, // default would have been the title
handler: File($$.Env.runningScript), // would be the default handler if not explicitly set
}
);
if( !ok )
{
msg = __("%1 > Couldn't install the menu action.", callee.µ);
(+$$.warn) ? $$.warn(msg) : $$.failure(msg);
this.OkAction = false;
return;
}
if( !this.OkAction )
{
msg = __("%1 > Action successfully installed.", callee.µ);
(+$$.trace) ? $$.trace(msg) : $$.success(msg);
this.OkAction = true;
}
},
onRunning: function onRunning_( µ,ev,tg,sel,tx,canRun,F)
//----------------------------------
{
µ = callee.µ; // more agnostic than `this`
if( !µ.OkAction ) return; // failure already notified
ev = $$.globalEvent();
if( !ev ) // re-executed from the scripts panel
{
// Here you could implement a yes/no dialog
// with the option to uninstall the action etc.
return;
}
tg = ev.target;
if( (!tg.isValid) || µ.UniqueLabel !== tg.properties.label )
{
// This shouldn't happen, unless crazy event
// managers have been installed in parallel...
$$.warn(__("%1 > Invalid or foreign event target!"), µ);
return;
}
// Checkpoint. Make sure this part runs *fast*
// to minimize latency on `beforeDisplay`.
sel = app.properties.selection||0;
sel = 1===sel.length && sel[0];
canRun = sel.isValid && sel.hasOwnProperty('texts')
&& 0 < (tx=sel.texts[0]).contents.length;
switch( ev.eventType )
{
case 'beforeDisplay':
// Enable/disable the menu action according to the context.
tg.enabled = !!canRun;
break;
case 'onInvoke':
if( canRun )
{
// Make the command undo-able.
// The way we pass the Text paramater to ~.CAML allows
// us to send a pure function to `app.doScript`, without
// literal arguments. The present script would then support
// JsxBlind obfuscation (in JSXBIN mode.)
(F = µ['~'].CAML).TEXT = tx;
app.doScript(F, void 0, void 0, +UndoModes.ENTIRE_SCRIPT, tg.name);
}
break;
default:;
}
},
});
$$.load(0);
try{ $$.CamelCase() }
catch(e){ $$.receiveError(e) }
$$.unload();