Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contextitems/ve.ui.MobileActionsContextItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor MobileActionsContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item to show extra actions required on mobile.
9 *
10 * These actions allow users to do things that are not possible with
11 * a virtual keyboard and a fake selection, specifically copy & delete.
12 *
13 * @class
14 * @extends ve.ui.LinearContextItem
15 *
16 * @param {ve.ui.LinearContext} context Context the item is in
17 * @param {ve.dm.Model} model Model the item is related to
18 * @param {Object} [config] Configuration options
19 */
20 ve.ui.MobileActionsContextItem = function VeUiMobileActionsContextItem( context, model, config ) {
21 // Parent constructor
22 ve.ui.MobileActionsContextItem.super.call( this, context, model, config );
23
24 this.copyButton = new OO.ui.ButtonWidget( {
25 framed: false,
26 label: ve.msg( 'visualeditor-clipboard-copy' ),
27 icon: 'copy'
28 } );
29 this.deleteButton = new OO.ui.ButtonWidget( {
30 framed: false,
31 label: ve.msg( 'visualeditor-contextitemwidget-label-remove' ),
32 icon: 'trash',
33 flags: [ 'destructive' ]
34 } );
35
36 this.$head.append( this.copyButton.$element );
37 if ( !this.isReadOnly() ) {
38 this.$head.append( this.deleteButton.$element );
39 }
40
41 // Events
42 this.copyButton.connect( this, { click: 'onCopyButtonClick' } );
43 this.deleteButton.connect( this, { click: 'onDeleteButtonClick' } );
44
45 // Initialization
46 this.$element.addClass( 've-ui-mobileActionsContextItem' );
47 };
48
49 /* Inheritance */
50
51 OO.inheritClass( ve.ui.MobileActionsContextItem, ve.ui.LinearContextItem );
52
53 /* Static Properties */
54
55 ve.ui.MobileActionsContextItem.static.name = 'mobileActions';
56
57 ve.ui.MobileActionsContextItem.static.editable = false;
58
59 // Show this context last
60 ve.ui.MobileActionsContextItem.static.sortOrder = 1;
61
62 /**
63 * @inheritdoc
64 */
65 ve.ui.MobileActionsContextItem.static.isCompatibleWith = function ( model ) {
66 return OO.ui.isMobile() && model instanceof ve.dm.Node && (
67 model.isFocusable() || model.isCellable()
68 );
69 };
70
71 /* Methods */
72
73 /**
74 * Handle copy button click events.
75 */
76 ve.ui.MobileActionsContextItem.prototype.onCopyButtonClick = function () {
77 const surfaceView = this.context.getSurface().getView();
78
79 surfaceView.activate();
80 // Force a native selection on mobile
81 surfaceView.preparePasteTargetForCopy( true );
82
83 let copied;
84 try {
85 copied = document.execCommand( 'copy' );
86 } catch ( e ) {
87 copied = false;
88 }
89
90 ve.init.platform.notify( ve.msg( copied ? 'visualeditor-clipboard-copy-success' : 'visualeditor-clipboard-copy-fail' ) );
91
92 // Restore normal selection for device type
93 surfaceView.preparePasteTargetForCopy();
94 if ( OO.ui.isMobile() ) {
95 // Support: Mobile Safari
96 // Force remove the selection to hide the keyboard
97 document.activeElement.blur();
98 }
99
100 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-copy' } );
101 };
102
103 /**
104 * Handle delete button click events.
105 *
106 * @fires ve.ui.ContextItem#command
107 */
108 ve.ui.MobileActionsContextItem.prototype.onDeleteButtonClick = function () {
109 const surface = this.context.getSurface();
110 const command = surface.commandRegistry.lookup( 'backspace' );
111
112 // Use the 'backspace' command as this triggers the KeyDownHandler for the
113 // current selection, e.g. ve.ce.TableDeleteKeyDownHandler will be used to
114 // clear table cells for TableSelection's.
115 if ( command ) {
116 command.execute( surface, undefined, 'context' );
117 this.emit( 'command' );
118 ve.track( 'activity.' + this.constructor.static.name, { action: 'context-delete' } );
119 }
120 };
121
122 /* Registration */
123
124 ve.ui.contextItemFactory.register( ve.ui.MobileActionsContextItem );