Skip to content
This repository was archived by the owner on Dec 13, 2017. It is now read-only.

Commit 14d4b81

Browse files
committed
Merge pull request #820 from wordpress-mobile/feature/keyboard-shortcuts
Added hardware keyboard shortcuts for iPad
2 parents 4a52c95 + 30aef16 commit 14d4b81

3 files changed

Lines changed: 126 additions & 6 deletions

File tree

Classes/WPEditorFormatbarView.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ typedef enum
147147
*/
148148
- (void)toolBarItemWithTag:(WPEditorViewControllerElementTag)tag setVisible:(BOOL)visible;
149149

150+
/**
151+
* @brief Selects or deselects a toolbar item
152+
*
153+
* @param tag WPEditorViewControllerElementTag of the item to alter.
154+
* @param selected YES to make the item selected, NO to deselect it.
155+
*/
156+
- (void)toolBarItemWithTag:(WPEditorViewControllerElementTag)tag setSelected:(BOOL)selected;
157+
158+
/**
159+
* @brief Toggles the on / off selection state for a toolbar item
160+
*
161+
* @param tag WPEditorViewControllerElementTag of the item to alter.
162+
*/
163+
- (void)toggleSelectionForToolBarItemWithTag:(WPEditorViewControllerElementTag)tag;
164+
150165
/**
151166
* @brief Enables and disables the toolbar items.
152167
*

Classes/WPEditorFormatbarView.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,36 @@ - (void)toolBarItemWithTag:(WPEditorViewControllerElementTag)tag setVisible:(BOO
203203
}
204204
}
205205

206+
- (void)toolBarItemWithTag:(WPEditorViewControllerElementTag)tag setSelected:(BOOL)selected
207+
{
208+
for (ZSSBarButtonItem *item in self.leftToolbar.items) {
209+
if (item.tag == tag) {
210+
item.selected = selected;
211+
}
212+
}
213+
214+
for (ZSSBarButtonItem *item in self.regularToolbar.items) {
215+
if (item.tag == tag) {
216+
item.selected = selected;
217+
}
218+
}
219+
}
220+
221+
- (void)toggleSelectionForToolBarItemWithTag:(WPEditorViewControllerElementTag)tag
222+
{
223+
for (ZSSBarButtonItem *item in self.leftToolbar.items) {
224+
if (item.tag == tag) {
225+
item.selected = !item.selected;
226+
}
227+
}
228+
229+
for (ZSSBarButtonItem *item in self.regularToolbar.items) {
230+
if (item.tag == tag) {
231+
item.selected = !item.selected;
232+
}
233+
}
234+
}
235+
206236
- (void)enableToolbarItems:(BOOL)enable
207237
shouldShowSourceButton:(BOOL)showSource
208238
{

Classes/WPEditorViewController.m

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ @interface WPEditorViewController () <HRColorPickerViewControllerDelegate, WPEdi
2424
#pragma mark - Properties: Editing
2525
@property (nonatomic, assign, readwrite, getter=isEditingEnabled) BOOL editingEnabled;
2626
@property (nonatomic, assign, readwrite, getter=isEditing) BOOL editing;
27+
@property (nonatomic, assign, readwrite, getter=isEditingTitle) BOOL editingTitle;
2728
@property (nonatomic, assign, readwrite) BOOL wasEditing;
2829

2930
#pragma mark - Properties: Editor View
@@ -174,6 +175,73 @@ - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withT
174175
[self.toolbarView setNeedsLayout];
175176
}
176177

178+
#pragma mark - Keyboard shortcuts
179+
180+
- (BOOL)canBecomeFirstResponder
181+
{
182+
return YES;
183+
}
184+
185+
- (NSArray<UIKeyCommand *> *)keyCommands
186+
{
187+
if (self.isEditingTitle) {
188+
return @[];
189+
}
190+
191+
// Note that due to an iOS 9 bug, the custom methods for bold and italic
192+
// don't actually get called: http://www.openradar.me/25463955
193+
return @[
194+
[UIKeyCommand keyCommandWithInput:@"B"
195+
modifierFlags:UIKeyModifierCommand
196+
action:@selector(setBold)
197+
discoverabilityTitle:NSLocalizedString(@"Bold", @"Discoverability title for bold formatting keyboard shortcut.")],
198+
[UIKeyCommand keyCommandWithInput:@"I"
199+
modifierFlags:UIKeyModifierCommand
200+
action:@selector(setItalic)
201+
discoverabilityTitle:NSLocalizedString(@"Italic", @"Discoverability title for italic formatting keyboard shortcut.")],
202+
[UIKeyCommand keyCommandWithInput:@"D"
203+
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate
204+
action:@selector(handleKeyCommandStrikethrough)
205+
discoverabilityTitle:NSLocalizedString(@"Strikethrough", @"Discoverability title for strikethrough formatting keyboard shortcut.")],
206+
[UIKeyCommand keyCommandWithInput:@"U"
207+
modifierFlags:UIKeyModifierCommand
208+
action:@selector(setUnderline)
209+
discoverabilityTitle:NSLocalizedString(@"Underline", @"Discoverability title for underline formatting keyboard shortcut.")],
210+
[UIKeyCommand keyCommandWithInput:@"Q"
211+
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate
212+
action:@selector(setBlockQuote)
213+
discoverabilityTitle:NSLocalizedString(@"Block Quote", @"Discoverability title for block quote keyboard shortcut.")],
214+
[UIKeyCommand keyCommandWithInput:@"K"
215+
modifierFlags:UIKeyModifierCommand
216+
action:@selector(linkBarButtonTapped)
217+
discoverabilityTitle:NSLocalizedString(@"Insert Link", @"Discoverability title for insert link keyboard shortcut.")],
218+
[UIKeyCommand keyCommandWithInput:@"M"
219+
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate
220+
action:@selector(didTouchMediaOptions)
221+
discoverabilityTitle:NSLocalizedString(@"Insert Media", @"Discoverability title for insert media keyboard shortcut.")],
222+
[UIKeyCommand keyCommandWithInput:@"U"
223+
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate
224+
action:@selector(setUnorderedList)
225+
discoverabilityTitle:NSLocalizedString(@"Bullet List", @"Discoverability title for bullet list keyboard shortcut.")],
226+
[UIKeyCommand keyCommandWithInput:@"O"
227+
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate
228+
action:@selector(setOrderedList)
229+
discoverabilityTitle:NSLocalizedString(@"Numbered List", @"Discoverability title for numbered list keyboard shortcut.")],
230+
[UIKeyCommand keyCommandWithInput:@"H"
231+
modifierFlags:UIKeyModifierCommand|UIKeyModifierShift
232+
action:@selector(showHTMLSource:)
233+
discoverabilityTitle:NSLocalizedString(@"Toggle HTML Source ", @"Discoverability title for HTML keyboard shortcut.")]
234+
];
235+
}
236+
237+
- (void)handleKeyCommandStrikethrough
238+
{
239+
[self setStrikethrough];
240+
241+
// Ensure that the toolbar button is appropriately selected / deselected
242+
[self.toolbarView toggleSelectionForToolBarItemWithTag:kWPEditorViewControllerElementStrikeThroughBarButton];
243+
}
244+
177245
#pragma mark - Toolbar: helper methods
178246

179247
- (void)clearToolbar
@@ -428,24 +496,27 @@ - (void)editorToolbarView:(WPEditorFormatbarView*)editorToolbarView
428496
- (void)editorToolbarView:(WPEditorFormatbarView*)editorToolbarView
429497
insertLink:(UIBarButtonItem *)barButtonItem
430498
{
431-
[self linkBarButtonTapped:(WPEditorToolbarButton *)barButtonItem];
499+
[self linkBarButtonTapped];
432500
}
433501

434502
#pragma mark - Editor Interaction
435503

436504
- (void)showHTMLSource:(UIBarButtonItem *)barButtonItem
437-
{
505+
{
438506
if ([self.editorView isInVisualMode]) {
439507
if ([self askOurDelegateShouldDisplaySourceView]) {
440508
[self.editorView showHTMLSource];
441-
barButtonItem.tintColor = [self barButtonItemSelectedDefaultColor];
509+
[self.toolbarView toolBarItemWithTag:kWPEditorViewControllerElementShowSourceBarButton
510+
setSelected:YES];
442511
} else {
443512
// Deselect the HTML button so it is in the proper state
444-
[(UIButton *)barButtonItem setSelected:NO];
513+
[self.toolbarView toolBarItemWithTag:kWPEditorViewControllerElementShowSourceBarButton
514+
setSelected:NO];
445515
}
446516
} else {
447517
[self.editorView showVisualEditor];
448-
barButtonItem.tintColor = [self.toolbarView itemTintColor];
518+
[self.toolbarView toolBarItemWithTag:kWPEditorViewControllerElementShowSourceBarButton
519+
setSelected:NO];
449520
}
450521

451522
[WPAnalytics track:WPAnalyticsStatEditorTappedHTML];
@@ -621,7 +692,7 @@ - (void)redo:(ZSSBarButtonItem *)barButtonItem
621692
[self.editorView redo];
622693
}
623694

624-
- (void)linkBarButtonTapped:(WPEditorToolbarButton*)button
695+
- (void)linkBarButtonTapped
625696
{
626697
if ([self.editorView isSelectionALink]) {
627698
[self removeLink];
@@ -901,9 +972,11 @@ - (void)editorView:(WPEditorView*)editorView
901972
{
902973
[self.toolbarView enableToolbarItems:NO shouldShowSourceButton:YES];
903974
if (field == self.editorView.titleField) {
975+
self.editingTitle = YES;
904976
[self.toolbarView enableToolbarItems:NO shouldShowSourceButton:YES];
905977
[self tellOurDelegateFormatBarStatusHasChanged:NO];
906978
} else {
979+
self.editingTitle = NO;
907980
[self.toolbarView enableToolbarItems:YES shouldShowSourceButton:YES];
908981
[self tellOurDelegateFormatBarStatusHasChanged:YES];
909982
}
@@ -912,8 +985,10 @@ - (void)editorView:(WPEditorView*)editorView
912985
- (void)editorView:(WPEditorView*)editorView sourceFieldFocused:(UIView*)view
913986
{
914987
if (view == self.editorView.sourceViewTitleField) {
988+
self.editingTitle = YES;
915989
[self.toolbarView enableToolbarItems:NO shouldShowSourceButton:YES];
916990
} else {
991+
self.editingTitle = NO;
917992
[self.toolbarView enableToolbarItems:YES shouldShowSourceButton:YES];
918993
}
919994
}

0 commit comments

Comments
 (0)