Debian Codegraph @

extensions/DiscussionTools/modules/dt.ui.ReplyWidgetVisual.js

raw symbols SCIP
1 const CommentTargetWidget = require( './dt-ve/CommentTargetWidget.js' );
2
3 require( './dt-ve/dt.ui.MWSignatureContextItem.js' );
4 require( './dt-ve/dt.dm.MWSignatureNode.js' );
5 require( './dt-ve/dt.ce.MWSignatureNode.js' );
6 require( './dt-ve/dt.ui.UsernameCompletionAction.js' );
7 require( './dt-ve/dt.ui.UsernameCompletionTool.js' );
8 require( './dt-ve/dt.dm.PingNode.js' );
9 require( './dt-ve/dt.ce.PingNode.js' );
10
11 /**
12 * DiscussionTools ReplyWidgetVisual class
13 *
14 * @class mw.dt.ReplyWidgetVisual
15 * @extends mw.dt.ReplyWidget
16 * @constructor
17 * @param {CommentController} commentController
18 * @param {CommentDetails} commentDetails
19 * @param {Object} [config]
20 * @param {string} [config.mode] Default edit mode, 'source' or 'visual'
21 */
22 function ReplyWidgetVisual( commentController, commentDetails, config ) {
23 this.defaultMode = config.mode;
24
25 // Parent constructor
26 ReplyWidgetVisual.super.apply( this, arguments );
27
28 // TODO: Rename this widget to VE, as it isn't just visual mode
29 this.$element.addClass( 'ext-discussiontools-ui-replyWidget-ve' );
30 }
31
32 /* Inheritance */
33
34 OO.inheritClass( ReplyWidgetVisual, require( './dt.ui.ReplyWidget.js' ) );
35
36 /* Methods */
37
38 /**
39 * @inheritdoc
40 */
41 ReplyWidgetVisual.prototype.createReplyBodyWidget = function ( config ) {
42 return new CommentTargetWidget( this, Object.assign( {
43 defaultMode: this.defaultMode
44 }, config ) );
45 };
46
47 /**
48 * @inheritdoc
49 */
50 ReplyWidgetVisual.prototype.getValue = function () {
51 if ( this.getMode() === 'source' ) {
52 return this.replyBodyWidget.target.getSurface().getModel().getDom();
53 } else {
54 return this.replyBodyWidget.target.getSurface().getHtml();
55 }
56 };
57
58 /**
59 * @inheritdoc
60 */
61 ReplyWidgetVisual.prototype.clear = function ( preserveStorage ) {
62 if ( !preserveStorage ) {
63 this.replyBodyWidget.target.clearDocState();
64 }
65 // #clear removes all the surfaces, so must be done after #clearDocState
66 this.replyBodyWidget.clear();
67
68 // Parent method
69 ReplyWidgetVisual.super.prototype.clear.apply( this, arguments );
70 };
71
72 /**
73 * @inheritdoc
74 */
75 ReplyWidgetVisual.prototype.isEmpty = function () {
76 const surface = this.replyBodyWidget.target.getSurface();
77 return !( surface && surface.getModel().getDocument().data.hasContent() );
78 };
79
80 /**
81 * @inheritdoc
82 */
83 ReplyWidgetVisual.prototype.getMode = function () {
84 return this.replyBodyWidget.target.getSurface() ?
85 this.replyBodyWidget.target.getSurface().getMode() :
86 this.defaultMode;
87 };
88
89 /**
90 * @inheritdoc
91 */
92 ReplyWidgetVisual.prototype.setup = function ( data, suppressNotifications ) {
93 const target = this.replyBodyWidget.target;
94
95 data = data || {};
96
97 let htmlOrDoc;
98 if ( this.storage.get( 'saveable' ) ) {
99 htmlOrDoc = this.storage.get( 've-dochtml' );
100 target.recovered = true;
101 } else {
102 htmlOrDoc = data.value;
103 }
104
105 htmlOrDoc = htmlOrDoc || ( this.getMode() === 'visual' ? '<p></p>' : '' );
106
107 target.originalHtml = htmlOrDoc instanceof HTMLDocument ? ve.properInnerHtml( htmlOrDoc.body ) : htmlOrDoc;
108 target.fromEditedState = !!data.value;
109
110 this.replyBodyWidget.setDocument( htmlOrDoc );
111
112 target.once( 'surfaceReady', () => {
113 target.getSurface().getView().connect( this, {
114 focus: [ 'emit', 'bodyFocus' ]
115 } );
116
117 target.initAutosave( {
118 suppressNotifications: suppressNotifications,
119 storage: this.storage
120 } );
121 this.afterSetup();
122
123 // This needs to bind after surfaceReady so any initial population doesn't trigger it early:
124 this.replyBodyWidget.once( 'change', this.onFirstChange.bind( this ) );
125 } );
126
127 // Parent method
128 ReplyWidgetVisual.super.prototype.setup.apply( this, arguments );
129
130 // Events
131 this.replyBodyWidget.connect( this, {
132 change: 'onInputChangeThrottled',
133 cancel: 'tryTeardown',
134 submit: 'onReplyClick'
135 } );
136
137 return this;
138 };
139
140 /**
141 * @inheritdoc
142 */
143 ReplyWidgetVisual.prototype.teardown = function () {
144 this.replyBodyWidget.disconnect( this );
145 this.replyBodyWidget.off( 'change' );
146
147 // Parent method
148 return ReplyWidgetVisual.super.prototype.teardown.apply( this, arguments );
149 };
150
151 /**
152 * @inheritdoc
153 */
154 ReplyWidgetVisual.prototype.focus = function () {
155 const targetWidget = this.replyBodyWidget;
156 setTimeout( () => {
157 // Check surface still exists after timeout
158 if ( targetWidget.getSurface() ) {
159 targetWidget.getSurface().getView().selectLastSelectableContentOffset();
160 targetWidget.focus();
161 }
162 } );
163
164 return this;
165 };
166
167 module.exports = ReplyWidgetVisual;