-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from qvantor/feature/variables
Feature/variables
- Loading branch information
Showing
42 changed files
with
674 additions
and
178 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
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: 2 additions & 0 deletions
2
apps/wavemail/src/app/mail-builder/mail-builder/components/mail-builder.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { TextEditorStyle } from '@waveditors/text-editor'; | ||
import { VersionsProvider } from '../../versions'; | ||
import { MailBuilderVersion } from './mail-builder-version'; | ||
|
||
export const MailBuilder = () => ( | ||
<VersionsProvider> | ||
<TextEditorStyle /> | ||
<MailBuilderVersion /> | ||
</VersionsProvider> | ||
); |
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
68 changes: 68 additions & 0 deletions
68
...tor-model/src/common/modules/element-variables/__snapshots__/apply-variables.spec.ts.snap
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,68 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`apply variables should change the var name in text content, if name is changed 1`] = ` | ||
Object { | ||
"id": "0.47976090123156934", | ||
"link": null, | ||
"name": "text1", | ||
"params": Object { | ||
"content": Object { | ||
"content": Array [ | ||
Object { | ||
"content": Array [ | ||
Object { | ||
"text": "hello ", | ||
"type": "text", | ||
}, | ||
Object { | ||
"attrs": Object { | ||
"id": "0.7830225981794139", | ||
"label": "Hello world", | ||
}, | ||
"type": "variable", | ||
}, | ||
Object { | ||
"text": " ", | ||
"type": "text", | ||
}, | ||
], | ||
"type": "paragraph", | ||
}, | ||
], | ||
"type": "doc", | ||
}, | ||
}, | ||
"style": Object {}, | ||
"type": "text", | ||
} | ||
`; | ||
|
||
exports[`apply variables should remove variable, if variable gone 1`] = ` | ||
Object { | ||
"id": "0.47976090123156934", | ||
"link": null, | ||
"name": "text1", | ||
"params": Object { | ||
"content": Object { | ||
"content": Array [ | ||
Object { | ||
"content": Array [ | ||
Object { | ||
"text": "hello ", | ||
"type": "text", | ||
}, | ||
Object { | ||
"text": " ", | ||
"type": "text", | ||
}, | ||
], | ||
"type": "paragraph", | ||
}, | ||
], | ||
"type": "doc", | ||
}, | ||
}, | ||
"style": Object {}, | ||
"type": "text", | ||
} | ||
`; |
88 changes: 88 additions & 0 deletions
88
libs/editor-model/src/common/modules/element-variables/apply-variables-to-element.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,88 @@ | ||
import { | ||
isTipTapVariable, | ||
mapTipTapParentsContent, | ||
TipTapVariable, | ||
TipTapVarLabel, | ||
} from '@waveditors/utils'; | ||
import { JSONContent } from '@tiptap/core'; | ||
import { | ||
ElementCommon, | ||
ElementLinkUrl, | ||
getElementLinkUrl, | ||
isTextElement, | ||
Text, | ||
TextContent, | ||
} from '../../../elements'; | ||
import { Variable, Variables } from '../../../variables'; | ||
|
||
const applyVariableToTipTapVariable = ( | ||
variable: Variable, | ||
tipTapVar: TipTapVariable | ||
): TipTapVariable | null => { | ||
if (variable.label === tipTapVar.attrs.label) return null; | ||
return TipTapVarLabel.set(variable.label)(tipTapVar); | ||
}; | ||
|
||
const applyVariablesToJSONContent = ( | ||
variables: Variables, | ||
content: JSONContent | ||
): JSONContent | null => { | ||
let changed = false; | ||
const result = mapTipTapParentsContent(content, (items) => | ||
items.reduce<JSONContent[]>((sum, item) => { | ||
if (!isTipTapVariable(item)) return [...sum, item]; | ||
const variable = variables.find( | ||
(variable) => variable.id === item.attrs.id | ||
); | ||
|
||
// no variables mean that var was removed | ||
if (!variable) { | ||
changed = true; | ||
return sum; | ||
} | ||
const newItem = applyVariableToTipTapVariable(variable, item); | ||
if (!newItem) return [...sum, item]; | ||
|
||
changed = true; | ||
return [...sum, newItem]; | ||
}, []) | ||
); | ||
return changed ? result : null; | ||
}; | ||
|
||
const applyVariablesToCommonElement = ( | ||
variables: Variables, | ||
element: ElementCommon | ||
): ElementCommon | null => { | ||
const url = getElementLinkUrl(element); | ||
if (!url || typeof url === 'string') return null; | ||
const newUrl = applyVariablesToJSONContent(variables, url); | ||
if (!newUrl) return null; | ||
return ElementLinkUrl.set(newUrl)(element); | ||
}; | ||
|
||
const applyVariablesToText = ( | ||
variables: Variables, | ||
text: Text | ||
): Text | null => { | ||
const content = applyVariablesToJSONContent(variables, text.params.content); | ||
if (!content) return null; | ||
return TextContent.set(content)(text); | ||
}; | ||
|
||
const applyVariablesByType = ( | ||
variables: Variables, | ||
element: ElementCommon | ||
): ElementCommon | null => { | ||
if (isTextElement(element)) return applyVariablesToText(variables, element); | ||
return null; | ||
}; | ||
|
||
export const applyVariablesToElement = ( | ||
variables: Variables, | ||
element: ElementCommon | ||
) => { | ||
const common = applyVariablesToCommonElement(variables, element); | ||
const result = applyVariablesByType(variables, common ?? element); | ||
return result ?? common; | ||
}; |
27 changes: 27 additions & 0 deletions
27
libs/editor-model/src/common/modules/element-variables/apply-variables.spec.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,27 @@ | ||
import { BasicCase } from '../../../_tests'; | ||
import { applyVariablesToElement } from './apply-variables-to-element'; | ||
|
||
const { Template, TextWithNameVariable, NameVariable } = BasicCase; | ||
|
||
const TextWithNameVariableNode = Template.elements[TextWithNameVariable]; | ||
|
||
describe('apply variables', () => { | ||
it('should do nothing, if variables the same', () => { | ||
expect( | ||
applyVariablesToElement([NameVariable], TextWithNameVariableNode) | ||
).toBeNull(); | ||
}); | ||
it('should change the var name in text content, if name is changed', () => { | ||
expect( | ||
applyVariablesToElement( | ||
[{ ...NameVariable, label: 'Hello world' }], | ||
TextWithNameVariableNode | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
it('should remove variable, if variable gone', () => { | ||
expect( | ||
applyVariablesToElement([], TextWithNameVariableNode) | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.