feat: feature/mvp-sprint-1 Progress on fixing contenteditable input and string value rendering issue

This commit is contained in:
Nikolaj Frey 2023-08-13 16:14:40 +10:00
parent 6663e12ebb
commit 753f06b3d6
4 changed files with 27 additions and 67 deletions

View File

@ -167,7 +167,7 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
newThingtime.tt = newThingtime newThingtime.tt = newThingtime
console.log( console.log(
"nik setting newThingtime value at path", "ThingtimeProvider setting newThingtime value at path",
'"' + path + '"', '"' + path + '"',
"value: ", "value: ",
value value
@ -191,7 +191,7 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
// do we need to sanitise? // do we need to sanitise?
// const path = sanitise(rawPath) // const path = sanitise(rawPath)
console.log("Getting thingtime at path", path) console.log("ThingtimeProvider getting thingtime at path", path)
// console.trace("Getting thingtime at path", path) // console.trace("Getting thingtime at path", path)
return smarts.getsmart(thingtime, path) return smarts.getsmart(thingtime, path)
}, },
@ -235,7 +235,10 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
if (thingtimeFromLocalStorage) { if (thingtimeFromLocalStorage) {
const parsed = parse(thingtimeFromLocalStorage) const parsed = parse(thingtimeFromLocalStorage)
console.log("thingtime restored from localstorage", parsed) console.log(
"ThingtimeProvider thingtime restored from localstorage",
parsed
)
if (parsed) { if (parsed) {
const localIsValid = const localIsValid =
!parsed.version || parsed.version >= force.version !parsed.version || parsed.version >= force.version
@ -246,7 +249,10 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
const withVersionUpdates = smarts.merge(newVersionData, parsed) const withVersionUpdates = smarts.merge(newVersionData, parsed)
newThingtime = smarts.merge(force, withVersionUpdates) newThingtime = smarts.merge(force, withVersionUpdates)
} }
console.log("restoring thingtime from localStorage", newThingtime) console.log(
"ThingtimeProvider restoring thingtime from localStorage",
newThingtime
)
set(newThingtime) set(newThingtime)
} }
} else { } else {
@ -281,7 +287,10 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
} }
} else { } else {
try { try {
console.log("Setting thingtime to localStorage", thingtime) console.log(
"ThingtimeProvider setting thingtime to localStorage",
thingtime
)
// setTimeout(() => { // setTimeout(() => {
const stringified = stringify(thingtime) const stringified = stringify(thingtime)
window.localStorage.setItem("thingtime", stringified) window.localStorage.setItem("thingtime", stringified)

View File

@ -294,8 +294,6 @@ export const Commander = (props) => {
if (curSuggestionIdx !== null) { if (curSuggestionIdx !== null) {
selectSuggestion(curSuggestionIdx) selectSuggestion(curSuggestionIdx)
} }
console.log("nik commanderActive", commanderActive)
console.log("nik commandIsAction", commandIsAction)
if (commanderActive) { if (commanderActive) {
try { try {
if (commandIsAction) { if (commandIsAction) {

View File

@ -109,6 +109,12 @@ export const MagicInput = (props) => {
[props?.onFocus] [props?.onFocus]
) )
const dangerouslySetInnerHTML = React.useMemo(() => {
if (!props?.readonly) {
return { __html: contentEditableValue }
}
// return { __html: contentEditableValue }
}, [contentEditableValue, props?.readonly])
return ( return (
<Box <Box
position="relative" position="relative"
@ -121,8 +127,8 @@ export const MagicInput = (props) => {
width="100%" width="100%"
border="none" border="none"
outline="none" outline="none"
contentEditable={true} contentEditable={!props?.readonly ? true : false}
dangerouslySetInnerHTML={{ __html: contentEditableValue }} dangerouslySetInnerHTML={dangerouslySetInnerHTML}
onFocus={onFocus} onFocus={onFocus}
onInput={(value) => { onInput={(value) => {
const innerText = value?.target?.innerText const innerText = value?.target?.innerText
@ -134,7 +140,9 @@ export const MagicInput = (props) => {
updateValue({ value: innerText }) updateValue({ value: innerText })
} }
}} }}
></Box> >
{props?.readonly ? props?.value : null}
</Box>
<Box <Box
position="absolute" position="absolute"
top={0} top={0}

View File

@ -36,7 +36,6 @@ export const Thingtime = (props) => {
const [circular, setCircular] = React.useState(props?.circular) const [circular, setCircular] = React.useState(props?.circular)
const contentEditableRef = React.useRef(null)
const editValueRef = React.useRef({}) const editValueRef = React.useRef({})
const depth = React.useMemo(() => { const depth = React.useMemo(() => {
@ -196,12 +195,7 @@ export const Thingtime = (props) => {
const renderableValue = React.useMemo(() => { const renderableValue = React.useMemo(() => {
if (type === "string") { if (type === "string") {
const trimmed = thing.trim() return <MagicInput value={thing} readonly></MagicInput>
if (!trimmed) {
return ""
}
return trimmed
} else if (type === "number") { } else if (type === "number") {
return thing return thing
} else if (type === "boolean") { } else if (type === "boolean") {
@ -328,6 +322,7 @@ export const Thingtime = (props) => {
) )
} }
}, [ }, [
AtomicWrapper,
keysToUse, keysToUse,
circular, circular,
seen, seen,
@ -342,59 +337,10 @@ export const Thingtime = (props) => {
keys, keys,
]) ])
const [contentEditableThing, setContentEditableThing] = React.useState(thing)
const updateContentEditableThing = React.useCallback((value) => {
// replace all new line occurences in value with <div><br></div>
// extract all series of new lines
const newlines = value?.split?.(/[^\n]/)?.filter((v) => v !== "")
let newValue = value
// replace all new lines groups with <div><br></div>
newlines?.forEach?.((newline) => {
const baseLength = "\n"?.length
const newlineClone = newline
const newlineClonePart1 = newlineClone?.replace(
"\n\n\n",
"<div><br /></div>"
)
const newlineClonePart2 = newlineClonePart1?.replace(
/\n\n/g,
"<div><br /></div>"
)
const newlineClonePart3 = newlineClonePart2?.replace(/\n/g, "<br />")
newValue = newValue?.replace(newline, newlineClonePart3)
})
setContentEditableThing(newValue)
}, [])
React.useEffect(() => {
const entries = Object.entries(editValueRef.current)
const propsThingInEntries = entries?.find?.(
(entry) => entry[1] === props?.thing
)
if (!propsThingInEntries) {
updateContentEditableThing(props?.thing)
// setContentEditableThing(props?.thing)
} else {
const [time, value] = propsThingInEntries
if (time && value) {
delete editValueRef.current[time]
}
}
}, [props?.thing, updateContentEditableThing])
const updateValue = React.useCallback( const updateValue = React.useCallback(
(args) => { (args) => {
const { value } = args const { value } = args
console.log("nik running updateValue", value)
setThingtime(fullPath, value) setThingtime(fullPath, value)
}, },
[fullPath, setThingtime] [fullPath, setThingtime]
@ -505,7 +451,6 @@ export const Thingtime = (props) => {
</AtomicWrapper> </AtomicWrapper>
) )
}, [ }, [
contentEditableThing,
renderableValue, renderableValue,
pl, pl,
type, type,