Fix tag editor preview issue due to blackslash (#2708)

Co-authored-by: Timothy Lim <tim.lim@intercom.io>
Co-authored-by: gatzjames <jamesgatzos@gmail.com>
This commit is contained in:
Timothy Lim 2021-09-09 22:42:04 +08:00 committed by GitHub
parent cb17482c76
commit 614957c2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -77,6 +77,13 @@ class TagEditor extends PureComponent<Props, State> {
activeTagData.rawValue = this.props.defaultValue;
}
// Fix strings: arg.value expects an escaped value (based on _updateArg logic)
for (const arg of activeTagData.args) {
if (typeof arg.value === 'string') {
arg.value = this._escapeStringArgs(arg.value);
}
}
await Promise.all([
this._refreshModels(this.props.workspace),
this._update(tagDefinitions, activeTagDefinition, activeTagData, true),
@ -178,7 +185,7 @@ class TagEditor extends PureComponent<Props, State> {
// Fix strings
if (typeof argValue === 'string') {
argValue = argValue.replace(/\\/g, '\\\\');
argValue = this._escapeStringArgs(argValue);
}
// Ensure all arguments exist
@ -324,6 +331,14 @@ class TagEditor extends PureComponent<Props, State> {
}, 100);
}
_escapeStringArgs(value: string) {
return value.replace(/\\/g, '\\\\');
}
_unescapeStringArgs(value: string) {
return value.replace(/\\\\/g, '\\');
}
static _getDefaultTagData(tagDefinition: NunjucksParsedTag): NunjucksParsedTag {
const defaultFill: string = templateUtils.getDefaultFill(
tagDefinition.name,
@ -425,7 +440,7 @@ class TagEditor extends PureComponent<Props, State> {
return (
<input
type="text"
defaultValue={value || ''}
defaultValue={this._unescapeStringArgs(value) || ''}
placeholder={placeholder}
onChange={this._handleChange}
data-encoding={encoding || 'utf8'}
@ -460,7 +475,7 @@ class TagEditor extends PureComponent<Props, State> {
showFileName
className="btn btn--clicky btn--super-compact"
onChange={path => this._handleChangeFile(path, argIndex)}
path={value}
path={this._unescapeStringArgs(value)}
itemtypes={itemTypes}
extensions={extensions}
/>