Skip to content

Commit

Permalink
User Suggested edit - workaround for preserving tabs
Browse files Browse the repository at this point in the history
Markedjs by default changes leading tabs to spaces. More details:
markedjs/marked#1559

As workaround we extract user suggestion from content of
gr-formatted-text which has tabs and not spaces.

Release-Notes: skip
Google-Bug-Id: b/279925682
Change-Id: I0a6d0223b384838b29753c41437b9174dda0da46
  • Loading branch information
milutin committed Apr 28, 2023
1 parent 2253500 commit a37ffe3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import '../gr-account-chip/gr-account-chip';
import '../gr-user-suggestion-fix/gr-user-suggestion-fix';
import {KnownExperimentId} from '../../../services/flags/flags';
import {getAppContext} from '../../../services/app-context';
import {USER_SUGGESTION_INFO_STRING} from '../../../utils/comment-util';
import {
getUserSuggestionFromString,
USER_SUGGESTION_INFO_STRING,
} from '../../../utils/comment-util';

/**
* This element optionally renders markdown and also applies some regex
Expand Down Expand Up @@ -298,9 +301,16 @@ export class GrFormattedText extends LitElement {
}

private convertCodeToSuggestions() {
for (const userSuggestionMark of this.renderRoot.querySelectorAll('mark')) {
const marks = this.renderRoot.querySelectorAll('mark');
if (marks.length > 0) {
const userSuggestionMark = marks[0];
const userSuggestion = document.createElement('gr-user-suggestion-fix');
userSuggestion.textContent = userSuggestionMark.textContent ?? '';
// Temporary workaround for bug - tabs replacement
if (this.content.includes('\t')) {
userSuggestion.textContent = getUserSuggestionFromString(this.content);
} else {
userSuggestion.textContent = userSuggestionMark.textContent ?? '';
}
userSuggestionMark.parentNode?.replaceChild(
userSuggestion,
userSuggestionMark
Expand Down
14 changes: 9 additions & 5 deletions polygerrit-ui/app/utils/comment-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,17 @@ export function hasUserSuggestion(comment: Comment) {
return comment.message?.includes(USER_SUGGESTION_START_PATTERN) ?? false;
}

export function getUserSuggestion(comment: Comment) {
if (!comment.message) return;
export function getUserSuggestionFromString(content: string) {
const start =
comment.message.indexOf(USER_SUGGESTION_START_PATTERN) +
content.indexOf(USER_SUGGESTION_START_PATTERN) +
USER_SUGGESTION_START_PATTERN.length;
const end = comment.message.indexOf('\n```', start);
return comment.message.substring(start, end);
const end = content.indexOf('\n```', start);
return content.substring(start, end);
}

export function getUserSuggestion(comment: Comment) {
if (!comment.message) return;
return getUserSuggestionFromString(comment.message);
}

export function getContentInCommentRange(
Expand Down

0 comments on commit a37ffe3

Please sign in to comment.