Fix strings

This commit is contained in:
barsdeveloper
2022-09-15 21:26:43 +02:00
parent 9f789b3e09
commit aa54bd0627
14 changed files with 100 additions and 150 deletions

View File

@@ -142,13 +142,6 @@ export default class Utility {
}
}
/**
* @param {String} value
*/
static firstCapital(value) {
return value.charAt(0).toUpperCase() + value.substring(1)
}
static getType(value) {
if (value === null) {
return null
@@ -209,47 +202,21 @@ export default class Utility {
return [...(new Set(result.concat(...a, ...b)))]
}
/**
* @param {String} value
*/
static encodeInputString(value) {
return value
.replace(/\n$/, "") // Remove trailing newline
.replaceAll("\u00A0", " ") // Replace special space symbol
.replaceAll("\n", String.raw`\r\n`) // Replace newline with \r\n
}
/**
* @param {String} value
*/
static decodeInputString(value) {
return value
.replaceAll("\\r\n", "\n") // Replace newline with \r\n
}
/**
* @param {String} value
*/
static encodeString(value, input = false) {
/** @param {String} value */
static escapeString(value, input = false) {
return value
.replaceAll('"', '\\"') // Escape "
.replaceAll("\n", "\\n") // Replace newline with \n
.replaceAll("\u00A0", " ") // Replace special space symbol
}
/**
* @param {String} value
*/
static decodeString(value, input = false) {
/** @param {String} value */
static unescapeString(value, input = false) {
return value
.replaceAll('\\"', '"')
.replaceAll("\\n", "\n")
.replaceAll(" ", "\u00A0")
}
/**
* @param {String} value
*/
/** @param {String} value */
static formatStringName(value) {
return value
.trim()
@@ -257,14 +224,8 @@ export default class Utility {
.replaceAll(/(?<=[a-z])(?=[A-Z])|_|\s+/g, " ") // Insert a space between a lowercase and uppercase letter, instead of an underscore or multiple spaces
}
/**
* @param {LinearColorEntity} value
*/
/** @param {LinearColorEntity} value */
static printLinearColor(value) {
return `${Math.round(value.R * 255)}, ${Math.round(value.G * 255)}, ${Math.round(value.B * 255)}`
}
static isFunction(value) {
return value instanceof Function && value.prototype === undefined
}
}

View File

@@ -20,19 +20,17 @@ import VectorPinTemplate from "../template/VectorPinTemplate"
* @typedef {import("./NodeElement").default} NodeElement
*/
/**
* @extends {IElement<PinEntity, PinTemplate>}
*/
/** @extends {IElement<PinEntity, PinTemplate>} */
export default class PinElement extends IElement {
static #typeTemplateMap = {
"/Script/CoreUObject.LinearColor": LinearColorPinTemplate,
"/Script/CoreUObject.Vector": VectorPinTemplate,
"bool": BoolPinTemplate,
"exec": ExecPinTemplate,
"name": NamePinTemplate,
"real": RealPinTemplate,
"string": StringPinTemplate,
"/Script/CoreUObject.LinearColor": LinearColorPinTemplate,
"/Script/CoreUObject.Vector": VectorPinTemplate,
}
static properties = {
@@ -96,6 +94,7 @@ export default class PinElement extends IElement {
get defaultValue() {
return this.unreactiveDefaultValue
}
/** @param {String} value */
set defaultValue(value) {
let oldValue = this.unreactiveDefaultValue
this.unreactiveDefaultValue = value
@@ -111,7 +110,11 @@ export default class PinElement extends IElement {
new (PinElement.getTypeTemplate(entity))()
)
this.advancedView = entity.bAdvancedView
/** @type {String} */
this.unreactiveDefaultValue = entity.getDefaultValue()
if (this.unreactiveDefaultValue.constructor === String) {
this.unreactiveDefaultValue = entity.getDefaultValue()
}
this.pinType = this.entity.getType()
this.color = this.constructor.properties.color.converter.fromAttribute(Configuration.pinColor[this.pinType]?.toString())
this.isLinked = false

View File

@@ -143,7 +143,7 @@ export default class Grammar {
Word = r => P.regex(/[a-zA-Z]+/).desc("a word")
String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')).map(Utility.decodeString)
String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')).map(Utility.unescapeString)
.desc('string (with possibility to escape the quote using \")')
ReferencePath = r => P.seq(

View File

@@ -23,7 +23,7 @@ export default class ToStringSerializer extends GeneralSerializer {
*/
write(entity, object, insideString) {
return !insideString && object.constructor === String
? `"${Utility.encodeString(object.toString())}"` // String will have quotes if not inside a string already
: Utility.encodeString(object.toString())
? `"${Utility.escapeString(object.toString())}"` // String will have quotes if not inside a string already
: Utility.escapeString(object.toString())
}
}

View File

@@ -134,8 +134,8 @@ export default function initializeSerializerFactory() {
String,
new CustomSerializer(
(value, insideString) => insideString
? Utility.encodeString(value)
: `"${Utility.encodeString(value)}"`,
? Utility.escapeString(value)
: `"${Utility.escapeString(value)}"`,
String
)
)

View File

@@ -1,7 +1,7 @@
import { html } from "lit"
import MouseIgnore from "../input/mouse/MouseIgnore"
import PinTemplate from "./PinTemplate"
import Utility from "../Utility"
import PinTemplate from "./PinTemplate"
/**
* @typedef {import("../element/PinElement").default} PinElement
@@ -15,6 +15,20 @@ export default class IInputPinTemplate extends PinTemplate {
return this.#inputContentElements
}
static stringFromInputToUE(value) {
return value
.replace(/(?=\n\s*)\n$/, "") // Remove trailing double newline
.replaceAll("\n", "\\r\n") // Replace newline with \r\n (default newline in UE)
}
static stringFromUEToInput(value) {
return value
.replaceAll(/(?:\r|(?<=(?:^|[^\\])(?:\\\\)*)\\r)(?=\n)/g, "") // Remove \r leftover from \r\n
.replace(/(?<=\n\s*)$/, "\n") // Put back trailing double newline
}
/**
* @param {PinElement} pin
* @param {Map} changedProperties
@@ -73,7 +87,10 @@ export default class IInputPinTemplate extends PinTemplate {
getInputs(pin) {
return this.#inputContentElements.map(element =>
// Faster than innerText which causes reflow
element.innerHTML.replaceAll("<br>", "\n"))
element.innerHTML
.replaceAll("&nbsp;", "\u00A0")
.replaceAll("<br>", "\n")
)
}
/**
@@ -81,9 +98,13 @@ export default class IInputPinTemplate extends PinTemplate {
* @param {String[]?} values
*/
setInputs(pin, values = [], updateDefaultValue = true) {
this.#inputContentElements.forEach((element, i) => element.innerText = values[i])
this.#inputContentElements.forEach(
(element, i) => element.innerText = values[i]
)
if (updateDefaultValue) {
pin.setDefaultValue(values.reduce((acc, cur) => acc + cur, ""))
pin.setDefaultValue(values
.map(v => IInputPinTemplate.stringFromInputToUE(v)) // Double newline at the end of a contenteditable element
.reduce((acc, cur) => acc + cur, ""))
}
}
@@ -94,7 +115,8 @@ export default class IInputPinTemplate extends PinTemplate {
if (pin.isInput()) {
return html`
<div class="ueb-pin-input">
<span class="ueb-pin-input-content" role="textbox" contenteditable="true" .innerText=${pin.unreactiveDefaultValue}></span>
<span class="ueb-pin-input-content" role="textbox" contenteditable="true"
.innerText="${IInputPinTemplate.stringFromUEToInput(pin.unreactiveDefaultValue.toString())}"></span>
</div>
`
}

View File

@@ -1,16 +1,4 @@
import IInputPinTemplate from "./IInputPinTemplate"
/**
* @typedef {import("../element/PinElement").default} PinElement
*/
export default class StringPinTemplate extends IInputPinTemplate {
/**
* @param {PinElement} pin
* @param {Map} changedProperties
*/
firstUpdated(pin, changedProperties) {
super.firstUpdated(pin, changedProperties)
}
}