All files / app/assets/javascripts/pages/shared/wikis/components wiki_form.vue

95.12% Statements 78/82
97.96% Branches 48/49
97.56% Functions 40/41
95.12% Lines 78/82

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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605  1x                   1x 1x 1x 1x 1x 1x 1x 1x               1x       1x                                                                                                                                                                                   2x                     59x                           124x 80x     59x     59x     59x           32x         27x     20x         59x         59x         85x 33x     24x     24x     66x     86x               59x     95x     85x         103x     95x       59x   59x     59x       22x   13x       10x 3x     10x       6x   6x 3x   3x     6x     6x   6x   6x       9x       25x   25x 25x 13x   12x                           4x   4x       8x   2x     2x 2x       103x     33x   33x 33x       15x       4x       5x 1x 1x   4x         13x       3x 3x         6x                     1x           1231x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
<script>
import {
  GlForm,
  GlIcon,
  GlLink,
  GlButton,
  GlSprintf,
  GlAlert,
  GlModal,
  GlModalDirective,
} from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import csrf from '~/lib/utils/csrf';
import { setUrlFragment } from '~/lib/utils/url_utility';
import { __, s__, sprintf } from '~/locale';
import Tracking from '~/tracking';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import {
  CONTENT_EDITOR_LOADED_ACTION,
  SAVED_USING_CONTENT_EDITOR_ACTION,
  WIKI_CONTENT_EDITOR_TRACKING_LABEL,
  WIKI_FORMAT_LABEL,
  WIKI_FORMAT_UPDATED_ACTION,
} from '../constants';
 
const trackingMixin = Tracking.mixin({
  label: WIKI_CONTENT_EDITOR_TRACKING_LABEL,
});
 
const MARKDOWN_LINK_TEXT = {
  markdown: '[Link Title](page-slug)',
  rdoc: '{Link title}[link:page-slug]',
  asciidoc: 'link:page-slug[Link title]',
  org: '[[page-slug]]',
};
 
export default {
  i18n: {
    title: {
      label: s__('WikiPage|Title'),
      placeholder: s__('WikiPage|Page title'),
      helpText: {
        existingPage: s__(
          'WikiPage|Tip: You can move this page by adding the path to the beginning of the title.',
        ),
        newPage: s__(
          'WikiPage|Tip: You can specify the full path for the new file. We will automatically create any missing directories.',
        ),
        learnMore: s__('WikiPage|Learn more.'),
      },
    },
    format: {
      label: s__('WikiPage|Format'),
    },
    content: {
      label: s__('WikiPage|Content'),
      placeholder: s__('WikiPage|Write your content or drag files hereā€¦'),
    },
    contentEditor: {
      renderFailed: {
        message: s__(
          'WikiPage|An error occured while trying to render the content editor. Please try again later.',
        ),
        primaryAction: s__('WikiPage|Retry'),
      },
      useNewEditor: {
        primaryLabel: s__('WikiPage|Use the new editor'),
        secondaryLabel: s__('WikiPage|Try this later'),
        title: s__('WikiPage|Get a richer editing experience'),
        text: s__(
          "WikiPage|Try the new visual Markdown editor. Read the %{linkStart}documentation%{linkEnd} to learn what's currently supported.",
        ),
      },
      switchToOldEditor: {
        label: s__('WikiPage|Switch me back to the classic editor.'),
        helpText: s__(
          "WikiPage|This editor is in beta and may not display the page's contents properly. Switching back to the classic editor will discard changes you've made in the new editor.",
        ),
        modal: {
          title: s__('WikiPage|Are you sure you want to switch back to the classic editor?'),
          primary: s__('WikiPage|Switch to classic editor'),
          cancel: s__('WikiPage|Keep editing'),
          text: s__(
            "WikiPage|Switching to the classic editor will discard any changes you've made in the new editor.",
          ),
        },
      },
      feedbackTip: __(
        'Tell us your experiences with the new Markdown editor %{linkStart}in this feedback issue%{linkEnd}.',
      ),
    },
    linksHelpText: s__(
      'WikiPage|To link to a (new) page, simply type %{linkExample}. More examples are in the %{linkStart}documentation%{linkEnd}.',
    ),
    commitMessage: {
      label: s__('WikiPage|Commit message'),
      value: {
        existingPage: s__('WikiPage|Update %{pageTitle}'),
        newPage: s__('WikiPage|Create %{pageTitle}'),
      },
    },
    submitButton: {
      existingPage: s__('WikiPage|Save changes'),
      newPage: s__('WikiPage|Create page'),
    },
    cancel: s__('WikiPage|Cancel'),
    editSourceButtonText: s__('WikiPage|Edit source'),
    editRichTextButtonText: s__('WikiPage|Edit rich text'),
  },
  contentEditorFeedbackIssue: 'https://gitlab.com/gitlab-org/gitlab/-/issues/332629',
  components: {
    GlAlert,
    GlForm,
    GlSprintf,
    GlIcon,
    GlLink,
    GlButton,
    GlModal,
    MarkdownField,
    ContentEditor: () =>
      import(
        /* webpackChunkName: 'content_editor' */ '~/content_editor/components/content_editor.vue'
      ),
  },
  directives: {
    GlModalDirective,
  },
  mixins: [trackingMixin, glFeatureFlagMixin()],
  inject: ['formatOptions', 'pageInfo'],
  data() {
    return {
      title: this.pageInfo.title?.trim() || '',
      format: this.pageInfo.format || 'markdown',
      content: this.pageInfo.content || '',
      isContentEditorAlertDismissed: false,
      useContentEditor: false,
      commitMessage: '',
      isDirty: false,
      contentEditorRenderFailed: false,
      contentEditorEmpty: false,
    };
  },
  computed: {
    noContent() {
      if (this.isContentEditorActive) return this.contentEditorEmpty;
      return !this.content.trim();
    },
    csrfToken() {
      return csrf.token;
    },
    formAction() {
      return this.pageInfo.persisted ? this.pageInfo.path : this.pageInfo.createPath;
    },
    helpPath() {
      return setUrlFragment(
        this.pageInfo.helpPath,
        this.pageInfo.persisted ? 'move-a-wiki-page' : 'create-a-new-wiki-page',
      );
    },
    commitMessageI18n() {
      return this.pageInfo.persisted
        ? this.$options.i18n.commitMessage.value.existingPage
        : this.$options.i18n.commitMessage.value.newPage;
    },
    linkExample() {
      return MARKDOWN_LINK_TEXT[this.format];
    },
    toggleEditingModeButtonText() {
      return this.isContentEditorActive
        ? this.$options.i18n.editSourceButtonText
        : this.$options.i18n.editRichTextButtonText;
    },
    submitButtonText() {
      return this.pageInfo.persisted
        ? this.$options.i18n.submitButton.existingPage
        : this.$options.i18n.submitButton.newPage;
    },
    titleHelpText() {
      return this.pageInfo.persisted
        ? this.$options.i18n.title.helpText.existingPage
        : this.$options.i18n.title.helpText.newPage;
    },
    cancelFormPath() {
      if (this.pageInfo.persisted) return this.pageInfo.path;
      return this.pageInfo.wikiPath;
    },
    wikiSpecificMarkdownHelpPath() {
      return setUrlFragment(this.pageInfo.markdownHelpPath, 'wiki-specific-markdown');
    },
    contentEditorHelpPath() {
      return setUrlFragment(this.pageInfo.helpPath, 'gitlab-flavored-markdown-support');
    },
    isMarkdownFormat() {
      return this.format === 'markdown';
    },
    showContentEditorAlert() {
      return (
        !this.glFeatures.wikiSwitchBetweenContentEditorRawMarkdown &&
        this.isMarkdownFormat &&
        !this.useContentEditor &&
        !this.isContentEditorAlertDismissed
      );
    },
    showSwitchEditingModeButton() {
      return this.glFeatures.wikiSwitchBetweenContentEditorRawMarkdown && this.isMarkdownFormat;
    },
    displayWikiSpecificMarkdownHelp() {
      return !this.isContentEditorActive;
    },
    displaySwitchBackToClassicEditorMessage() {
      return (
        !this.glFeatures.wikiSwitchBetweenContentEditorRawMarkdown && this.isContentEditorActive
      );
    },
    disableSubmitButton() {
      return this.noContent || !this.title || this.contentEditorRenderFailed;
    },
    isContentEditorActive() {
      return this.isMarkdownFormat && this.useContentEditor;
    },
  },
  mounted() {
    this.updateCommitMessage();
 
    window.addEventListener('beforeunload', this.onPageUnload);
  },
  destroyed() {
    window.removeEventListener('beforeunload', this.onPageUnload);
  },
  methods: {
    renderMarkdown(content) {
      return axios
        .post(this.pageInfo.markdownPreviewPath, { text: content })
        .then(({ data }) => data.body);
    },
 
    toggleEditingMode() {
      if (this.useContentEditor) {
        this.content = this.contentEditor.getSerializedContent();
      }
 
      this.useContentEditor = !this.useContentEditor;
    },
 
    async handleFormSubmit(e) {
      e.preventDefault();
 
      if (this.useContentEditor) {
        this.content = this.contentEditor.getSerializedContent();
 
        this.trackFormSubmit();
      }
 
      this.trackWikiFormat();
 
      // Wait until form field values are refreshed
      await this.$nextTick();
 
      e.target.submit();
 
      this.isDirty = false;
    },
 
    handleContentChange() {
      this.isDirty = true;
    },
 
    async loadInitialContent(contentEditor) {
      this.contentEditor = contentEditor;
 
      try {
        await this.contentEditor.setSerializedContent(this.content);
        this.trackContentEditorLoaded();
      } catch (e) {
        this.contentEditorRenderFailed = true;
      }
    },
 
    async retryInitContentEditor() {
      try {
        this.contentEditorRenderFailed = false;
        await this.contentEditor.setSerializedContent(this.content);
      } catch (e) {
        this.contentEditorRenderFailed = true;
      }
    },
 
    handleContentEditorChange({ empty }) {
      this.contentEditorEmpty = empty;
      // TODO: Implement a precise mechanism to detect changes in the Content
      this.isDirty = true;
    },
 
    onPageUnload(event) {
      if (!this.isDirty) return undefined;
 
      event.preventDefault();
 
      // eslint-disable-next-line no-param-reassign
      event.returnValue = '';
      return '';
    },
 
    updateCommitMessage() {
      if (!this.title) return;
 
      // Replace hyphens with spaces
      const newTitle = this.title.replace(/-+/g, ' ');
 
      const newCommitMessage = sprintf(this.commitMessageI18n, { pageTitle: newTitle }, false);
      this.commitMessage = newCommitMessage;
    },
 
    initContentEditor() {
      this.useContentEditor = true;
    },
 
    switchToOldEditor() {
      this.useContentEditor = false;
    },
 
    confirmSwitchToOldEditor() {
      if (this.contentEditorRenderFailed) {
        this.contentEditorRenderFailed = false;
        this.switchToOldEditor();
      } else {
        this.$refs.confirmSwitchToOldEditorModal.show();
      }
    },
 
    trackContentEditorLoaded() {
      this.track(CONTENT_EDITOR_LOADED_ACTION);
    },
 
    trackFormSubmit() {
E      if (this.isContentEditorActive) {
        this.track(SAVED_USING_CONTENT_EDITOR_ACTION);
      }
    },
 
    trackWikiFormat() {
      this.track(WIKI_FORMAT_UPDATED_ACTION, {
        label: WIKI_FORMAT_LABEL,
        extra: {
          project_path: this.pageInfo.path,
          old_format: this.pageInfo.format,
          value: this.format,
        },
      });
    },
 
    dismissContentEditorAlert() {
      this.isContentEditorAlertDismissed = true;
    },
  },
};
</script>
 
<template>
  <gl-form
    :action="formAction"
    method="post"
    class="wiki-form common-note-form gl-mt-3 js-quick-submit"
    @submit="handleFormSubmit"
  >
    <gl-alert
      v-if="isContentEditorActive && contentEditorRenderFailed"
      class="gl-mb-6"
      :dismissible="false"
      variant="danger"
      :primary-button-text="$options.i18n.contentEditor.renderFailed.primaryAction"
      @primaryAction="retryInitContentEditor"
    >
      {{ $options.i18n.contentEditor.renderFailed.message }}
    </gl-alert>
 
    <input :value="csrfToken" type="hidden" name="authenticity_token" />
    <input v-if="pageInfo.persisted" type="hidden" name="_method" value="put" />
    <input
      :v-if="pageInfo.persisted"
      type="hidden"
      name="wiki[last_commit_sha]"
      :value="pageInfo.lastCommitSha"
    />
    <div class="form-group row">
      <div class="col-sm-2 col-form-label">
        <label class="control-label-full-width" for="wiki_title">{{
          $options.i18n.title.label
        }}</label>
      </div>
      <div class="col-sm-10">
        <input
          id="wiki_title"
          v-model="title"
          name="wiki[title]"
          type="text"
          class="form-control"
          data-qa-selector="wiki_title_textbox"
          :required="true"
          :autofocus="!pageInfo.persisted"
          :placeholder="$options.i18n.title.placeholder"
          @input="updateCommitMessage"
        />
        <span class="gl-display-inline-block gl-max-w-full gl-mt-2 gl-text-gray-600">
          <gl-icon class="gl-mr-n1" name="bulb" />
          {{ titleHelpText }}
          <gl-link :href="helpPath" target="_blank">
            {{ $options.i18n.title.helpText.learnMore }}
          </gl-link>
        </span>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-sm-2 col-form-label">
        <label class="control-label-full-width" for="wiki_format">{{
          $options.i18n.format.label
        }}</label>
      </div>
      <div class="col-sm-10">
        <select
          id="wiki_format"
          v-model="format"
          class="form-control"
          name="wiki[format]"
          :disabled="isContentEditorActive"
        >
          <option v-for="(key, label) of formatOptions" :key="key" :value="key">
            {{ label }}
          </option>
        </select>
      </div>
    </div>
    <div class="form-group row" data-testid="wiki-form-content-fieldset">
      <div class="col-sm-2 col-form-label">
        <label class="control-label-full-width" for="wiki_content">{{
          $options.i18n.content.label
        }}</label>
      </div>
      <div class="col-sm-10">
        <div
          v-if="showSwitchEditingModeButton"
          class="gl-display-flex gl-justify-content-end gl-mb-3"
        >
          <gl-button
            data-testid="toggle-editing-mode-button"
            data-qa-selector="editing_mode_button"
            :data-qa-mode="toggleEditingModeButtonText"
            variant="link"
            @click="toggleEditingMode"
            >{{ toggleEditingModeButtonText }}</gl-button
          >
        </div>
        <gl-alert
          v-if="showContentEditorAlert"
          class="gl-mb-6"
          variant="info"
          data-qa-selector="try_new_editor_container"
          :primary-button-text="$options.i18n.contentEditor.useNewEditor.primaryLabel"
          :secondary-button-text="$options.i18n.contentEditor.useNewEditor.secondaryLabel"
          :dismiss-label="$options.i18n.contentEditor.useNewEditor.secondaryLabel"
          :title="$options.i18n.contentEditor.useNewEditor.title"
          @primaryAction="initContentEditor"
          @secondaryAction="dismissContentEditorAlert"
          @dismiss="dismissContentEditorAlert"
        >
          <gl-sprintf :message="$options.i18n.contentEditor.useNewEditor.text">
            <template
              #link="// eslint-disable-next-line vue/no-template-shadow
                { content }"
              ><gl-link
                :href="contentEditorHelpPath"
                target="_blank"
                data-testid="content-editor-help-link"
                >{{ content }}</gl-link
              ></template
            >
          </gl-sprintf>
        </gl-alert>
        <gl-modal
          ref="confirmSwitchToOldEditorModal"
          modal-id="confirm-switch-to-old-editor"
          :title="$options.i18n.contentEditor.switchToOldEditor.modal.title"
          :action-primary="{ text: $options.i18n.contentEditor.switchToOldEditor.modal.primary }"
          :action-cancel="{ text: $options.i18n.contentEditor.switchToOldEditor.modal.cancel }"
          @primary="switchToOldEditor"
        >
          {{ $options.i18n.contentEditor.switchToOldEditor.modal.text }}
        </gl-modal>
        <markdown-field
          v-if="!isContentEditorActive"
          :markdown-preview-path="pageInfo.markdownPreviewPath"
          :can-attach-file="true"
          :enable-autocomplete="true"
          :textarea-value="content"
          :markdown-docs-path="pageInfo.markdownHelpPath"
          :uploads-path="pageInfo.uploadsPath"
          class="bordered-box"
        >
          <template #textarea>
            <textarea
              id="wiki_content"
              ref="textarea"
              v-model="content"
              name="wiki[content]"
              class="note-textarea js-gfm-input js-autosize markdown-area"
              dir="auto"
              data-supports-quick-actions="false"
              data-qa-selector="wiki_content_textarea"
              :autofocus="pageInfo.persisted"
              :aria-label="$options.i18n.content.label"
              :placeholder="$options.i18n.content.placeholder"
              @input="handleContentChange"
            >
            </textarea>
          </template>
        </markdown-field>
 
        <div v-if="isContentEditorActive">
          <gl-alert class="gl-mb-6" variant="tip" :dismissible="false">
            <gl-sprintf :message="$options.i18n.contentEditor.feedbackTip">
              <template
                #link="// eslint-disable-next-line vue/no-template-shadow
                { content }"
                ><gl-link
                  :href="$options.contentEditorFeedbackIssue"
                  target="_blank"
                  data-testid="wiki-markdown-help-link"
                  >{{ content }}</gl-link
                ></template
              >
            </gl-sprintf>
          </gl-alert>
          <content-editor
            :render-markdown="renderMarkdown"
            :uploads-path="pageInfo.uploadsPath"
            @initialized="loadInitialContent"
            @change="handleContentEditorChange"
          />
          <input id="wiki_content" v-model.trim="content" type="hidden" name="wiki[content]" />
        </div>
 
        <div class="clearfix"></div>
        <div class="error-alert"></div>
 
        <div class="form-text gl-text-gray-600">
          <gl-sprintf v-if="displayWikiSpecificMarkdownHelp" :message="$options.i18n.linksHelpText">
            <template #linkExample
              ><code>{{ linkExample }}</code></template
            >
            <template
              #link="// eslint-disable-next-line vue/no-template-shadow
                { content }"
              ><gl-link
                :href="wikiSpecificMarkdownHelpPath"
                target="_blank"
                data-testid="wiki-markdown-help-link"
                >{{ content }}</gl-link
              ></template
            >
          </gl-sprintf>
          <span v-if="displaySwitchBackToClassicEditorMessage">
            {{ $options.i18n.contentEditor.switchToOldEditor.helpText }}
            <gl-button variant="link" @click="confirmSwitchToOldEditor">{{
              $options.i18n.contentEditor.switchToOldEditor.label
            }}</gl-button>
          </span>
        </div>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-sm-2 col-form-label">
        <label class="control-label-full-width" for="wiki_message">{{
          $options.i18n.commitMessage.label
        }}</label>
      </div>
      <div class="col-sm-10">
        <input
          id="wiki_message"
          v-model.trim="commitMessage"
          name="wiki[message]"
          type="text"
          class="form-control"
          data-qa-selector="wiki_message_textbox"
          :placeholder="$options.i18n.commitMessage.label"
        />
      </div>
    </div>
    <div class="form-actions">
      <gl-button
        category="primary"
        variant="confirm"
        type="submit"
        data-qa-selector="wiki_submit_button"
        data-testid="wiki-submit-button"
        :disabled="disableSubmitButton"
        >{{ submitButtonText }}</gl-button
      >
      <gl-button data-testid="wiki-cancel-button" :href="cancelFormPath" class="float-right">{{
        $options.i18n.cancel
      }}</gl-button>
    </div>
  </gl-form>
</template>