|
|
|
@ -1,4 +1,10 @@ |
|
|
|
import { store } from "./index" |
|
|
|
import { Helpers } from "@budibase/bbui" |
|
|
|
import { |
|
|
|
decodeJSBinding, |
|
|
|
encodeJSBinding, |
|
|
|
findAllBindings, |
|
|
|
} from "@budibase/string-templates" |
|
|
|
|
|
|
|
/** |
|
|
|
* Recursively searches for a specific component ID |
|
|
|
@ -161,3 +167,58 @@ export const getComponentSettings = componentType => { |
|
|
|
|
|
|
|
return settings |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Randomises a components ID's, including all child component IDs, and also |
|
|
|
* updates all data bindings to still be valid. |
|
|
|
* This mutates the object in place. |
|
|
|
* @param component the component to randomise |
|
|
|
*/ |
|
|
|
export const makeComponentUnique = component => { |
|
|
|
if (!component) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Replace component ID
|
|
|
|
const oldId = component._id |
|
|
|
const newId = Helpers.uuid() |
|
|
|
component._id = newId |
|
|
|
|
|
|
|
if (component._children?.length) { |
|
|
|
let children = JSON.stringify(component._children) |
|
|
|
|
|
|
|
// Replace all instances of this ID in child HBS bindings
|
|
|
|
children = children.replace(new RegExp(oldId, "g"), newId) |
|
|
|
|
|
|
|
// Replace all instances of this ID in child JS bindings
|
|
|
|
const bindings = findAllBindings(children) |
|
|
|
bindings.forEach(binding => { |
|
|
|
// JSON.stringify will have escaped double quotes, so we need
|
|
|
|
// to account for that
|
|
|
|
let sanitizedBinding = binding.replace(/\\"/g, '"') |
|
|
|
|
|
|
|
// Check if this is a valid JS binding
|
|
|
|
let js = decodeJSBinding(sanitizedBinding) |
|
|
|
if (js != null) { |
|
|
|
// Replace ID inside JS binding
|
|
|
|
js = js.replace(new RegExp(oldId, "g"), newId) |
|
|
|
|
|
|
|
// Create new valid JS binding
|
|
|
|
let newBinding = encodeJSBinding(js) |
|
|
|
|
|
|
|
// Replace escaped double quotes
|
|
|
|
newBinding = newBinding.replace(/"/g, '\\"') |
|
|
|
|
|
|
|
// Insert new JS back into binding.
|
|
|
|
// A single string replace here is better than a regex as
|
|
|
|
// the binding contains special characters, and we only need
|
|
|
|
// to replace a single instance.
|
|
|
|
children = children.replace(binding, newBinding) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
// Recurse on all children
|
|
|
|
component._children = JSON.parse(children) |
|
|
|
component._children.forEach(makeComponentUnique) |
|
|
|
} |
|
|
|
} |
|
|
|
|