| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.HelpCompletionAction = function ( surface ) { |
| 19 |
|
| 20 |
ve.ui.HelpCompletionAction.super.apply( this, arguments ); |
| 21 |
|
| 22 |
this.toolbar = surface.target.getToolbar(); |
| 23 |
this.tools = this.toolbar.tools; |
| 24 |
this.toolNames = Object.keys( this.tools ).filter( ( toolName ) => { |
| 25 |
const tool = this.tools[ toolName ]; |
| 26 |
return tool && |
| 27 |
|
| 28 |
!( tool instanceof ve.ui.HelpCompletionTool ) && |
| 29 |
|
| 30 |
!( tool instanceof OO.ui.ToolGroupTool ) && |
| 31 |
!( tool instanceof OO.ui.PopupTool ); |
| 32 |
} ); |
| 33 |
|
| 34 |
this.toolNames.sort( ( a, b ) => { |
| 35 |
const aGroup = this.tools[ a ].constructor.static.group; |
| 36 |
const bGroup = this.tools[ b ].constructor.static.group; |
| 37 |
if ( aGroup === bGroup ) { |
| 38 |
|
| 39 |
return 0; |
| 40 |
} |
| 41 |
if ( aGroup === 'format' ) { |
| 42 |
return 1; |
| 43 |
} |
| 44 |
if ( bGroup === 'format' ) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
return 0; |
| 49 |
} ); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
OO.inheritClass( ve.ui.HelpCompletionAction, ve.ui.CompletionAction ); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.ui.HelpCompletionAction.static.name = 'HelpCompletion'; |
| 59 |
|
| 60 |
ve.ui.HelpCompletionAction.static.alwaysIncludeInput = false; |
| 61 |
|
| 62 |
ve.ui.HelpCompletionAction.static.defaultLimit = 99; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ve.ui.HelpCompletionAction.static.toolGroups = { |
| 77 |
textStyle: { |
| 78 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-text-style' ) |
| 79 |
}, |
| 80 |
meta: { |
| 81 |
mergeWith: 'insert' |
| 82 |
}, |
| 83 |
object: { |
| 84 |
mergeWith: 'insert' |
| 85 |
}, |
| 86 |
format: { |
| 87 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-formatting' ), |
| 88 |
weight: -2 |
| 89 |
}, |
| 90 |
dialog: { |
| 91 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-dialog' ) |
| 92 |
}, |
| 93 |
other: { |
| 94 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-other' ), |
| 95 |
weight: -1 |
| 96 |
}, |
| 97 |
history: { |
| 98 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-history' ), |
| 99 |
weight: -3 |
| 100 |
}, |
| 101 |
structure: { |
| 102 |
title: OO.ui.deferMsg( 'visualeditor-toolbar-structure' ) |
| 103 |
}, |
| 104 |
insert: { |
| 105 |
title: OO.ui.deferMsg( 'visualeditor-shortcuts-insert' ), |
| 106 |
weight: 1 |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ve.ui.HelpCompletionAction.prototype.open = function ( isolateInput ) { |
| 113 |
if ( !isolateInput ) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
this.toolNames = this.toolNames.filter( ( toolName ) => { |
| 120 |
const tool = this.tools[ toolName ]; |
| 121 |
return !( tool instanceof ve.ui.HistoryTool ); |
| 122 |
} ); |
| 123 |
} |
| 124 |
|
| 125 |
return ve.ui.HelpCompletionAction.super.prototype.open.apply( this, arguments ); |
| 126 |
}; |
| 127 |
|
| 128 |
ve.ui.HelpCompletionAction.prototype.getToolIndex = function ( toolName ) { |
| 129 |
const tool = this.tools[ toolName ]; |
| 130 |
const toolGroups = this.constructor.static.toolGroups; |
| 131 |
const group = this.getGroupForTool( tool ); |
| 132 |
return OO.ui.resolveMsg( toolGroups[ group ].title ) + ' ' + tool.getTitle(); |
| 133 |
}; |
| 134 |
|
| 135 |
ve.ui.HelpCompletionAction.prototype.getSuggestions = function ( input ) { |
| 136 |
return ve.createDeferred().resolve( this.filterSuggestionsForInput( |
| 137 |
this.toolNames, |
| 138 |
input |
| 139 |
) ); |
| 140 |
}; |
| 141 |
|
| 142 |
ve.ui.HelpCompletionAction.prototype.compareSuggestionToInput = function ( suggestion, normalizedInput ) { |
| 143 |
const normalizedSuggestion = this.getToolIndex( suggestion ).toLowerCase(); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
let matchedIndex = 0; |
| 148 |
for ( let i = 0, l = normalizedInput.length; i < l; i++ ) { |
| 149 |
matchedIndex = normalizedSuggestion.indexOf( normalizedInput[ i ], matchedIndex ); |
| 150 |
if ( matchedIndex === -1 ) { |
| 151 |
return { |
| 152 |
isMatch: false, |
| 153 |
isExact: false |
| 154 |
}; |
| 155 |
} |
| 156 |
matchedIndex++; |
| 157 |
} |
| 158 |
|
| 159 |
return { |
| 160 |
isMatch: true, |
| 161 |
|
| 162 |
isExact: false |
| 163 |
}; |
| 164 |
}; |
| 165 |
|
| 166 |
ve.ui.HelpCompletionAction.prototype.getMenuItemForSuggestion = function ( toolName ) { |
| 167 |
const tool = this.tools[ toolName ]; |
| 168 |
return new OO.ui.MenuOptionWidget( { |
| 169 |
data: tool, |
| 170 |
label: tool.getTitle(), |
| 171 |
|
| 172 |
icon: tool.getIcon() || tool.constructor.static.fallbackIcon || '_', |
| 173 |
disabled: tool.isDisabled() |
| 174 |
} ); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
ve.ui.HelpCompletionAction.prototype.getGroupForTool = function ( tool ) { |
| 184 |
const toolGroups = this.constructor.static.toolGroups; |
| 185 |
let group = tool.constructor.static.group; |
| 186 |
if ( toolGroups[ group ] ) { |
| 187 |
if ( toolGroups[ group ].mergeWith ) { |
| 188 |
group = toolGroups[ group ].mergeWith; |
| 189 |
} |
| 190 |
} else { |
| 191 |
group = 'other'; |
| 192 |
} |
| 193 |
return group; |
| 194 |
}; |
| 195 |
|
| 196 |
ve.ui.HelpCompletionAction.prototype.updateMenuItems = function ( menuItems ) { |
| 197 |
const menuItemsByGroup = {}; |
| 198 |
const toolGroups = this.constructor.static.toolGroups; |
| 199 |
menuItems.forEach( ( menuItem ) => { |
| 200 |
const tool = menuItem.getData(); |
| 201 |
const group = this.getGroupForTool( tool ); |
| 202 |
menuItemsByGroup[ group ] = menuItemsByGroup[ group ] || []; |
| 203 |
menuItemsByGroup[ group ].push( menuItem ); |
| 204 |
} ); |
| 205 |
const newMenuItems = []; |
| 206 |
const groups = Object.keys( menuItemsByGroup ); |
| 207 |
groups.sort( ( a, b ) => { |
| 208 |
const weightA = toolGroups[ a ].weight || 0; |
| 209 |
const weightB = toolGroups[ b ].weight || 0; |
| 210 |
return weightB - weightA; |
| 211 |
} ); |
| 212 |
groups.forEach( ( group ) => { |
| 213 |
newMenuItems.push( |
| 214 |
new OO.ui.MenuSectionOptionWidget( { |
| 215 |
label: toolGroups[ group ].title |
| 216 |
} ) |
| 217 |
); |
| 218 |
ve.batchPush( newMenuItems, menuItemsByGroup[ group ] ); |
| 219 |
} ); |
| 220 |
return newMenuItems; |
| 221 |
}; |
| 222 |
|
| 223 |
ve.ui.HelpCompletionAction.prototype.chooseItem = function ( item, range ) { |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
const fragment = this.surface.getModel().getLinearFragment( range, true ); |
| 228 |
fragment.removeContent(); |
| 229 |
fragment.collapseToEnd(); |
| 230 |
|
| 231 |
const tool = item.getData(); |
| 232 |
|
| 233 |
|
| 234 |
setTimeout( () => { |
| 235 |
tool.onSelect(); |
| 236 |
} ); |
| 237 |
|
| 238 |
return true; |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.ui.actionFactory.register( ve.ui.HelpCompletionAction ); |
| 244 |
|
| 245 |
ve.ui.commandRegistry.register( new ve.ui.Command( |
| 246 |
'openHelpCompletions', ve.ui.HelpCompletionAction.static.name, 'open', |
| 247 |
{ supportedSelections: [ 'linear' ] } |
| 248 |
) ); |
| 249 |
ve.ui.commandRegistry.register( new ve.ui.Command( |
| 250 |
'openHelpCompletionsTrigger', ve.ui.HelpCompletionAction.static.name, 'open', |
| 251 |
{ supportedSelections: [ 'linear', 'table' ], args: [ true ] } |
| 252 |
) ); |
| 253 |
|
| 254 |
ve.ui.sequenceRegistry.register( new ve.ui.Sequence( 'autocompleteHelpCommands', 'openHelpCompletions', '\\', 0 ) ); |
| 255 |
|
| 256 |
ve.ui.triggerRegistry.register( |
| 257 |
'openHelpCompletionsTrigger', { |
| 258 |
|
| 259 |
mac: [ |
| 260 |
new ve.ui.Trigger( 'cmd+shift+p' ), |
| 261 |
new ve.ui.Trigger( 'cmd+alt+shift+p' ) |
| 262 |
], |
| 263 |
pc: [ |
| 264 |
new ve.ui.Trigger( 'ctrl+shift+p' ), |
| 265 |
new ve.ui.Trigger( 'ctrl+alt+shift+p' ) |
| 266 |
] |
| 267 |
} |
| 268 |
); |
| 269 |
|
| 270 |
ve.ui.commandHelpRegistry.register( 'other', 'openHelpCompletions', { |
| 271 |
trigger: 'openHelpCompletionsTrigger', |
| 272 |
sequences: [ 'autocompleteHelpCommands' ], |
| 273 |
label: OO.ui.deferMsg( 'visualeditor-toolbar-search-help-label' ) |
| 274 |
} ); |