-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into 4391-pan-european-reasonableness-chec…
…k-32-table-fix
- Loading branch information
Showing
193 changed files
with
3,764 additions
and
737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { useMemo, useRef } from 'react' | ||
|
||
import { useDOMChanges } from './hooks/useDOMChanges' | ||
import { cleanDOM } from './hooks/utils' | ||
import { DiffDOMProps } from './types' | ||
|
||
const DiffDOM: React.FC<DiffDOMProps> = (props) => { | ||
const { current = '', prev = '' } = props | ||
|
||
const ref = useRef<HTMLDivElement>() | ||
const __html = useMemo<string>(() => cleanDOM(prev), [prev]) | ||
useDOMChanges({ current, prev, ref }) | ||
|
||
return <div ref={ref} className="editorWYSIWYG jodit-wysiwyg diff-text" dangerouslySetInnerHTML={{ __html }} /> | ||
} | ||
|
||
export default DiffDOM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { DiffInfoAddElement, DiffType } from 'client/components/DiffDOM/types' | ||
|
||
import { applyDiffClassToElement } from './utils' | ||
|
||
export const addElement = (info: DiffInfoAddElement) => { | ||
// eslint-disable-next-line no-param-reassign | ||
info.diff.element = applyDiffClassToElement(info.diff.element, DiffType.added) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as Diff from 'diff' | ||
import { Strings } from 'utils/strings' | ||
|
||
export const getTextDiffNode = (currentValue: string, newValue: string): HTMLDivElement => { | ||
const changes = Diff.diffLines(Strings.nbspToUnicode(currentValue), Strings.nbspToUnicode(newValue)) | ||
|
||
const newNode = document.createElement('div') | ||
|
||
changes.forEach((change, i) => { | ||
const { added, removed, value } = change | ||
|
||
value.split('\n\r').forEach((text) => { | ||
const span = document.createElement('span') | ||
if (added) span.classList.add('added') | ||
if (removed) span.classList.add('removed') | ||
span.textContent = text | ||
newNode.appendChild(span) | ||
|
||
if (i < changes.length - 1) { | ||
newNode.appendChild(document.createElement('br')) | ||
} | ||
}) | ||
}) | ||
|
||
return newNode | ||
} |
49 changes: 49 additions & 0 deletions
49
src/client/components/DiffDOM/hooks/_postApplyRemoveElements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { MutableRefObject } from 'react' | ||
|
||
import { DiffDOM } from 'diff-dom' | ||
import { objToNode } from 'diff-dom/src/diffDOM/dom/fromVirtual' | ||
|
||
import { applyDiffClassToElement } from 'client/components/DiffDOM/hooks/utils' | ||
import { DiffInfoRemoveElement, DiffType } from 'client/components/DiffDOM/types' | ||
|
||
type Props = { | ||
diffDOM: DiffDOM | ||
diffs: Array<DiffInfoRemoveElement> | ||
ref: MutableRefObject<HTMLDivElement> | ||
} | ||
|
||
export const postApplyRemoveElements = (props: Props): void => { | ||
const { diffDOM, diffs, ref } = props | ||
// record of deleted nodes count by parent route | ||
const removedCounts: Record<string, number> = {} | ||
|
||
diffs.forEach((diffInfo) => { | ||
const { diff } = diffInfo | ||
const { element, route } = diff | ||
|
||
// 1. add 'removed' class and convert removed element to node | ||
const obj = applyDiffClassToElement(element, DiffType.removed) | ||
const node = objToNode(obj, false, diffDOM.options) | ||
|
||
// 2. get parent node from route | ||
const parentRoute = route.slice(0, -1) | ||
let parentNode: ChildNode = ref.current | ||
parentRoute.forEach((routeIndex, iterationIndex) => { | ||
const _parentRoute = route.slice(0, iterationIndex) | ||
const _removedKey = JSON.stringify(_parentRoute) | ||
const _removedCount = removedCounts[_removedKey] ?? 0 | ||
parentNode = parentNode?.childNodes?.[routeIndex + _removedCount] | ||
}) | ||
|
||
// 3. insert removed node at new index | ||
const removedKey = JSON.stringify(parentRoute) | ||
const removedCount = removedCounts[removedKey] ?? 0 | ||
if (parentNode) { | ||
const childIndex = route.slice(-1)?.at(0) | ||
parentNode.insertBefore(node, parentNode.childNodes[childIndex + removedCount]) | ||
} | ||
|
||
// 4. update parent route removed counts | ||
removedCounts[removedKey] = removedCount + 1 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DiffElementNode, DiffInfoReplaceElement, DiffType } from 'client/components/DiffDOM/types' | ||
|
||
import { applyDiffClassToElement } from './utils' | ||
|
||
export const replaceElement = (info: DiffInfoReplaceElement) => { | ||
const { oldValue } = info.diff | ||
const { newValue } = info.diff | ||
|
||
const newValueUpdated: DiffElementNode = { | ||
nodeName: 'div', | ||
attributes: { class: 'diff-text' }, | ||
childNodes: [ | ||
applyDiffClassToElement(oldValue, DiffType.removed), | ||
applyDiffClassToElement(newValue, DiffType.added), | ||
], | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
info.diff.newValue = newValueUpdated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useLayoutEffect } from 'react' | ||
|
||
import { DiffDOM, stringToObj } from 'diff-dom' | ||
|
||
import { | ||
DiffAction, | ||
DiffDOMProps, | ||
DiffInfo, | ||
DiffInfoAddElement, | ||
DiffInfoRemoveElement, | ||
DiffInfoReplaceElement, | ||
} from 'client/components/DiffDOM/types' | ||
|
||
import { addElement } from './_addElement' | ||
import { getTextDiffNode } from './_getTextDiffNode' | ||
import { postApplyRemoveElements } from './_postApplyRemoveElements' | ||
import { replaceElement } from './_replaceElement' | ||
import { normalizeDiffDOM } from './utils' | ||
|
||
type Props = DiffDOMProps & { | ||
ref: React.MutableRefObject<HTMLDivElement> | ||
} | ||
|
||
export const useDOMChanges = (props: Props) => { | ||
const { current, prev, ref } = props | ||
|
||
useLayoutEffect(() => { | ||
const removedDiffs: Array<DiffInfoRemoveElement> = [] | ||
|
||
const diffDOM = new DiffDOM({ | ||
caseSensitive: true, | ||
preDiffApply: (info: DiffInfo<DiffAction, unknown>): boolean => { | ||
switch (info.diff.action) { | ||
case DiffAction.replaceElement: | ||
replaceElement(info as DiffInfoReplaceElement) | ||
return false | ||
case DiffAction.addElement: | ||
addElement(info as DiffInfoAddElement) | ||
return false | ||
case DiffAction.removeElement: | ||
removedDiffs.push(info as DiffInfoRemoveElement) | ||
return false | ||
default: | ||
return false | ||
} | ||
}, | ||
textDiff(node, currentValue, _expectedValue, newValue) { | ||
if (node instanceof Text) { | ||
node.replaceWith(getTextDiffNode(currentValue, newValue)) | ||
} | ||
}, | ||
}) | ||
|
||
const normalizedPrev = normalizeDiffDOM(prev) | ||
const normalizedCurrent = normalizeDiffDOM(current) | ||
|
||
const objDiffPrev = stringToObj(normalizedPrev) | ||
const objDiffCurrent = stringToObj(normalizedCurrent) | ||
const diff = diffDOM.diff(objDiffPrev, objDiffCurrent) | ||
diffDOM.apply(ref.current, diff) | ||
|
||
// add removed nodes to dom | ||
postApplyRemoveElements({ diffDOM, diffs: removedDiffs, ref }) | ||
}, [current, prev, ref]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Strings } from 'utils/strings' | ||
|
||
import { DiffElement, DiffElementNode, DiffElementText, DiffType } from 'client/components/DiffDOM/types' | ||
|
||
export const cleanDOM = (value: string): string => { | ||
return ( | ||
value | ||
.replace(/[\n\t\r]/g, '') | ||
// .replace(/\u00a0| /g, ' ') | ||
.toString() | ||
) | ||
} | ||
|
||
export const normalizeDiffDOM = (value: string): string => { | ||
const cleanedValue = cleanDOM(value) | ||
|
||
const parser = new DOMParser() | ||
const doc = parser.parseFromString(`<div>${cleanedValue}</div>`, 'text/html') | ||
const element = doc.body.firstElementChild as HTMLElement | ||
element?.normalize() | ||
|
||
return element ? `<div class="___diff">${element.innerHTML}</div>` : '' | ||
} | ||
|
||
export const applyDiffClassToElement = (element: DiffElement, type: DiffType): DiffElementNode => { | ||
if (['#comment', '#text'].includes(element.nodeName)) { | ||
// replace space with Unicode char | ||
const data = Strings.nbspToUnicode((element as DiffElementText).data) | ||
const textNode = { nodeName: element.nodeName, data } | ||
|
||
return { nodeName: 'span', attributes: { class: type }, childNodes: [textNode] } | ||
} | ||
|
||
const elementNode = element as DiffElementNode | ||
return { | ||
...elementNode, | ||
childNodes: elementNode.childNodes?.map((child) => applyDiffClassToElement(child, type)) || [], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './DiffDOM' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export type DiffDOMProps = { | ||
current: string | ||
prev: string | ||
} | ||
|
||
export enum DiffType { | ||
added = 'added', | ||
removed = 'removed', | ||
} | ||
|
||
export type DiffElement = { nodeName: string } | ||
|
||
export type DiffElementNode = DiffElement & { | ||
attributes?: Record<string, string> | ||
checked?: boolean | ||
childNodes?: DiffElement[] | ||
selected?: boolean | ||
value?: string | number | ||
} | ||
|
||
export type DiffElementText = DiffElement & { | ||
data: string | ||
} | ||
|
||
export enum DiffAction { | ||
addElement = 'addElement', | ||
removeElement = 'removeElement', | ||
replaceElement = 'replaceElement', | ||
} | ||
|
||
type Diff<Action extends DiffAction, Props> = { | ||
action: Action | ||
route: Array<number> | ||
} & Props | ||
|
||
export type DiffInfo< | ||
Action extends DiffAction = DiffAction, | ||
Props extends Record<string, unknown> | void | unknown = void | ||
> = { | ||
diff: Diff<Action, Props> | ||
node: HTMLElement | ||
} | ||
|
||
export type DiffInfoAddElement = DiffInfo<DiffAction.addElement, { element: DiffElement }> | ||
export type DiffInfoRemoveElement = DiffInfo<DiffAction.removeElement, { element: DiffElement }> | ||
export type DiffInfoReplaceElement = DiffInfo< | ||
DiffAction.replaceElement, | ||
{ newValue: DiffElement; oldValue: DiffElement } | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@import 'src/client/style/partials'; | ||
|
||
.diff-text { | ||
span { | ||
word-break: break-word; | ||
} | ||
|
||
.added, | ||
.removed { | ||
font-weight: 600; | ||
} | ||
|
||
.added { | ||
color: darken($ui-status-accepted, 10%); | ||
} | ||
|
||
.removed { | ||
color: $ui-destructive; | ||
text-decoration: line-through; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import './DiffText.scss' | ||
import React from 'react' | ||
|
||
import classNames from 'classnames' | ||
import { Change } from 'diff' | ||
|
||
interface Props { | ||
changes: Array<Change> | ||
className?: string | ||
} | ||
|
||
const DiffText: React.FC<Props> = (props) => { | ||
const { changes, className = '' } = props | ||
|
||
return ( | ||
<div className={classNames('diff-text', className)}> | ||
{changes?.map((change, i) => { | ||
const { added, removed, value } = change | ||
const key = `${value}_${String(i)}` | ||
|
||
return ( | ||
<React.Fragment key={key}> | ||
{value.split('\n\r').map((text, j) => ( | ||
<React.Fragment key={`${key}_${String(j)}`}> | ||
<span className={classNames({ added, removed })}>{text}</span> | ||
<br /> | ||
</React.Fragment> | ||
))} | ||
</React.Fragment> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
export default DiffText |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './DiffText' |
2 changes: 1 addition & 1 deletion
2
src/client/components/Navigation/NavAssessment/History/Items/Item/props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/client/components/Navigation/NavAssessment/History/Items/hooks/useColumns.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/client/components/Navigation/NavAssessment/History/Items/utils/getStatePath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/client/components/Navigation/NavAssessment/History/Items/utils/getTargetValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.