All files / ext.wikilambda.app/components/widgets/publish PublishDialog.vue

100% Statements 363/363
85.71% Branches 24/28
71.42% Functions 5/7
100% Lines 363/363

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 3641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 17x 17x 17x 17x 17x 17x 36x 36x 36x 36x 36x 36x 36x 36x 68x 68x 68x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 16x 16x 16x 16x 16x 16x 15x 15x 15x 15x 15x 15x 16x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 36x 36x 36x 36x 36x 36x 36x 36x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 2x 2x 2x 2x 2x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 3x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 3x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 156x 156x 140x 36x 36x 36x 36x 36x 36x 36x 36x 24x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for the Publish Dialog.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div data-testid="confirm-publish-dialog">
		<cdx-dialog
			class="ext-wikilambda-app-publish-dialog"
			data-testid="publish-dialog"
			:open="showDialog"
			:title="i18n( 'wikilambda-editor-publish-dialog-header' ).text()"
			:close-button-label="i18n( 'wikilambda-dialog-close' ).text()"
			:use-close-button="true"
			:primary-action="primaryAction"
			:default-action="defaultAction"
			@default="closeDialog"
			@primary="publishPage"
			@update:open="closeDialog"
		>
			<!-- Error and Warning section -->
			<div
				v-if="hasErrors"
				class="ext-wikilambda-app-publish-dialog__errors"
			>
				<cdx-message
					v-for="( error, index ) in errors"
					:key="'dialog-error-' + index"
					class="ext-wikilambda-app-publish-dialog__error"
					:type="error.type"
				>
					<wl-safe-message :error="error"></wl-safe-message>
				</cdx-message>
			</div>
 
			<!-- Summary section -->
			<cdx-field :status="status">
				<cdx-text-input
					v-model="summary"
					class="ext-wikilambda-app-publish-dialog__summary-input"
					:aria-label="i18n( 'wikilambda-editor-publish-dialog-summary-label' ).text()"
					:placeholder="i18n( 'wikilambda-editor-publish-dialog-summary-placeholder' ).text()"
					@keydown="handleSummaryKeydown"
				></cdx-text-input>
				<template #label>
					{{ i18n( 'wikilambda-editor-publish-dialog-summary-help-text' ).text() }}
				</template>
 
				<template v-if="hasKeyboardSubmitWarning" #warning>
					<!-- eslint-disable-next-line vue/no-v-html -->
					<span v-html="keyboardSubmitMessage"></span>
				</template>
			</cdx-field>
 
			<!-- Legal text -->
			<template #footer-text>
				<!-- eslint-disable vue/no-v-html -->
				<div
					class="ext-wikilambda-app-publish-dialog__legal-text"
					v-html="legalText"
				></div>
				<!-- eslint-enable vue/no-v-html -->
			</template>
		</cdx-dialog>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, ref } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const useEventLog = require( '../../../composables/useEventLog.js' );
const useMainStore = require( '../../../store/index.js' );
 
// Base components:
const SafeMessage = require( '../../base/SafeMessage.vue' );
// Codex components
const { CdxDialog, CdxField, CdxMessage, CdxTextInput } = require( '../../../../codex.js' );
 
const enterKeyChar = `
	<kbd class="ext-wikilambda-app-publish-dialog__kbd">
		<span class="ext-wikilambda-app-publish-dialog__kbd-enter">&#x21B5;</span>
	</kbd>
`;
const cmdKeyChar = `
	<kbd class="ext-wikilambda-app-publish-dialog__kbd">
		<span class="ext-wikilambda-app-publish-dialog__kbd-cmd">&#x2318;</span>
	</kbd>
`;
 
const ctrlKeyChar = `
	<kbd class="ext-wikilambda-app-publish-dialog__kbd">
		<span class="ext-wikilambda-app-publish-dialog__kbd-ctrl">Ctrl</span>
	</kbd>
`;
module.exports = exports = defineComponent( {
	name: 'wl-publish-dialog',
	components: {
		'cdx-field': CdxField,
		'cdx-text-input': CdxTextInput,
		'cdx-message': CdxMessage,
		'cdx-dialog': CdxDialog,
		'wl-safe-message': SafeMessage
	},
	inject: {
		viewmode: { default: false }
	},
	props: {
		showDialog: {
			type: Boolean,
			required: true,
			default: false
		},
		customLegalText: {
			type: String,
			required: false,
			default: ''
		},
		submitAction: {
			type: Function,
			required: false,
			default: undefined
		},
		successCallback: {
			type: Function,
			required: false,
			default: () => {}
		},
		errorCallback: {
			type: Function,
			required: false,
			default: () => {}
		}
	},
	emits: [ 'before-exit', 'close-dialog' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const eventLogUtils = useEventLog();
		const store = useMainStore();
 
		// Dialog state
		const summary = ref( '' );
		const hasKeyboardSubmitWarning = ref( false );
		const isPublishing = ref( false );
 
		/**
		 * Clears the error notifications and emits a close-dialog
		 * event for the Publish widget to close the dialog.
		 */
		function closeDialog() {
			hasKeyboardSubmitWarning.value = false;
 
			// Clear all publish dialog errors/warnings (errorId: "main"), preserve field-level warnings
			store.clearErrors( Constants.STORED_OBJECTS.MAIN );
			emit( 'close-dialog' );
		}
 
		/**
		 * Returns an object of type PrimaryModalAction that describes
		 * the action of the primary (save or publish) dialog button.
		 *
		 * @return {Object}
		 */
		const primaryAction = computed( () => ( {
			actionType: 'progressive',
			label: i18n( 'wikilambda-publishnew' ).text(),
			disabled: isPublishing.value
		} ) );
 
		/**
		 * Returns an object of type ModalAction that describes
		 * the action of the secondary (cancel) button.
		 *
		 * @return {Object}
		 */
		const defaultAction = {
			label: i18n( 'wikilambda-cancel' ).text()
		};
 
		// Errors and warnings
		/**
		 * Returns the array of errors and warnings of the page
		 *
		 * @return {Array}
		 */
		const errors = computed( () => store.getErrors( Constants.STORED_OBJECTS.MAIN ) );
 
		/**
		 * Returns whether there are any errors in the page
		 * to show in the publish dialog
		 *
		 * @return { boolean }
		 */
		const hasErrors = computed( () => errors.value.length !== 0 );
 
		/**
		 * Returns the status of the summary text field.
		 *
		 * @return {string}
		 */
		const status = computed( () => hasKeyboardSubmitWarning.value ? 'warning' : 'default' );
 
		// Submission state
		/**
		 * Submits the page to the appropriate API and handles the return value:
		 * 1. If the response contains an error, saves the error
		 *    in the store/errors module and displays the error messages
		 *    in the Publish Dialog notification block.
		 * 2. If the response is successful, navigates to the content page.
		 */
		function publishPage() {
			if ( props.submitAction ) {
				isPublishing.value = true;
 
				props.submitAction( {
					summary: summary.value
				} ).then( ( response ) => {
					// Successful submission: push event, unset dirty and close
					emit( 'before-exit' );
					store.setDirty( false );
					closeDialog();
					// Finally, run additional success actions passed by parent
					props.successCallback( response );
				} ).catch( ( /* ApiError */ error ) => {
					props.errorCallback( error );
				} ).finally( () => {
					isPublishing.value = false;
					// TODO event submission for abstract content
					const interactionData = {
						zobjecttype: store.getCurrentZObjectType || null,
						zobjectid: store.getCurrentZObjectId || null,
						zlang: store.getUserLangZid || null,
						implementationtype: store.getCurrentZImplementationType || null,
						haserrors: hasErrors.value
					};
					eventLogUtils.submitInteraction( 'publish', interactionData );
				} );
			}
		}
 
		// Keyboard handling
		/**
		 * Handle pressing the Enter key on the summary field.
		 *
		 * @param {Event} event The keydown event.
		 */
		function handleSummaryEnter( event ) {
			event.preventDefault();
			hasKeyboardSubmitWarning.value = true;
		}
 
		/**
		 * Returns a warning message which informs the user that they can submit using Ctrl/CMD + Enter;
		 *
		 * @return {string}
		 */
		const keyboardSubmitMessage = computed( () => {
			const isMac = /Mac|iPod|iPhone|iPad/.test( navigator.userAgent );
			// Make sure the message is escaped, but insert the key HTML without escaping it
			return i18n( 'wikilambda-editor-publish-dialog-keyboard-submit-warning' ).escaped()
				.replace( '$1', isMac ? cmdKeyChar : ctrlKeyChar )
				.replace( '$2', enterKeyChar );
		} );
 
		/**
		 * Handles the keydown event on the summary text field.
		 * - If the user presses Ctrl/Cmd + Enter, publishes the page.
		 * - If the user presses Enter, shows a warning message.
		 *
		 * @param {Event} event The keydown event.
		 */
		function handleSummaryKeydown( event ) {
			const enterKey = event.key === 'Enter';
 
			// If the user presses Ctrl/Cmd + Enter, publish the page
			if ( ( event.metaKey || event.ctrlKey ) && enterKey ) {
				publishPage();
				return;
			}
 
			// If the user presses Enter, show a warning message
			if ( enterKey ) {
				handleSummaryEnter( event );
			}
		}
 
		// Legal text
		/**
		 * Returns the default legal text to display in the Publish Dialog, depending
		 * on the type of object that is being submitted:
		 * * Special message for implementations (Apache 2.0 licence for code).
		 * * General message for all other kinds of ZObjects (CC0).
		 *
		 * @return { string }
		 */
		const defaultLegalText = computed( () => (
			store.getCurrentZObjectType === Constants.Z_IMPLEMENTATION ?
				i18n( 'wikifunctions-editing-copyrightwarning-implementation' ).parse() :
				i18n( 'wikifunctions-editing-copyrightwarning-function' ).parse()
		) );
		/**
		 * Returns the legal text to display in the Publish Dialog,
		 * either the custom legal text provided by the parent component or the default legal text.
		 *
		 * @return { string }
		 */
		const legalText = computed( () => (
			props.customLegalText || defaultLegalText.value
		) );
 
		// Return all properties and methods for the template
		return {
			closeDialog,
			defaultAction,
			errors,
			handleSummaryKeydown,
			hasErrors,
			hasKeyboardSubmitWarning,
			i18n,
			keyboardSubmitMessage,
			legalText,
			primaryAction,
			publishPage,
			status,
			summary
		};
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-publish-dialog {
	.ext-wikilambda-app-publish-dialog__errors {
		margin-bottom: @spacing-200;
	}
 
	.ext-wikilambda-app-publish-dialog__kbd {
		font-size: @font-size-x-small;
		font-weight: @font-weight-normal;
		line-height: @line-height-x-small;
		color: @color-base;
		background-color: @background-color-interactive-subtle;
		border: @border-width-base @border-style-base @border-color-subtle;
		border-radius: @border-radius-base;
		padding: (@spacing-12 / 2) @spacing-25;
		box-shadow: @box-shadow-drop-small;
		font-family: inherit;
		display: inline-block;
	}
 
	.ext-wikilambda-app-publish-dialog__kbd-enter {
		position: relative;
		top: (@spacing-12 / 2);
	}
 
	.ext-wikilambda-app-publish-dialog__legal-text {
		color: @color-placeholder;
	}
}
</style>