-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone editor: Remove more dependencies (#2139)
* Standalone editor: Refactor list format handler 1 * Standalone editor: List refactor 2 * Standalone editor: remove dependencies --------- Co-authored-by: Bryan Valverde U <[email protected]>
- Loading branch information
1 parent
d0924ae
commit 573f78a
Showing
27 changed files
with
776 additions
and
69 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
5 changes: 2 additions & 3 deletions
5
packages-content-model/roosterjs-content-model-dom/lib/domUtils/metadata/updateMetadata.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
65 changes: 65 additions & 0 deletions
65
packages-content-model/roosterjs-content-model-dom/lib/domUtils/metadata/validate.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,65 @@ | ||
import { getObjectKeys } from '../getObjectKeys'; | ||
import type { Definition } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
* Validate the given object with a type definition object | ||
* @param input The object to validate | ||
* @param def The type definition object used for validation | ||
* @returns True if the object passed the validation, otherwise false | ||
*/ | ||
export function validate<T>(input: any, def: Definition<T>): input is T { | ||
let result = false; | ||
if ((def.isOptional && typeof input === 'undefined') || (def.allowNull && input === null)) { | ||
result = true; | ||
} else if ( | ||
(!def.isOptional && typeof input === 'undefined') || | ||
(!def.allowNull && input === null) | ||
) { | ||
return false; | ||
} else { | ||
switch (def.type) { | ||
case 'string': | ||
result = | ||
typeof input === 'string' && | ||
(typeof def.value === 'undefined' || input === def.value); | ||
break; | ||
|
||
case 'number': | ||
result = | ||
typeof input === 'number' && | ||
(typeof def.value === 'undefined' || areSameNumbers(def.value, input)) && | ||
(typeof def.minValue === 'undefined' || input >= def.minValue) && | ||
(typeof def.maxValue === 'undefined' || input <= def.maxValue); | ||
break; | ||
|
||
case 'boolean': | ||
result = | ||
typeof input === 'boolean' && | ||
(typeof def.value === 'undefined' || input === def.value); | ||
break; | ||
|
||
case 'array': | ||
result = | ||
Array.isArray(input) && | ||
(typeof def.minLength === 'undefined' || input.length >= def.minLength) && | ||
(typeof def.maxLength === 'undefined' || input.length <= def.maxLength) && | ||
input.every(x => validate(x, def.itemDef)); | ||
break; | ||
|
||
case 'object': | ||
result = | ||
typeof input === 'object' && | ||
getObjectKeys(def.propertyDef).every(x => | ||
validate(input[x], def.propertyDef[x]) | ||
); | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function areSameNumbers(n1: number, n2: number) { | ||
return Math.abs(n1 - n2) < 1e-3; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages-content-model/roosterjs-content-model-dom/lib/domUtils/unwrap.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,20 @@ | ||
/** | ||
* @internal | ||
* Removes the node and keep all children in place, return the parentNode where the children are attached | ||
* @param node the node to remove | ||
*/ | ||
export function unwrap(node: Node): Node | null { | ||
// Unwrap requires a parentNode | ||
const parentNode = node ? node.parentNode : null; | ||
|
||
if (!parentNode) { | ||
return null; | ||
} | ||
|
||
while (node.firstChild) { | ||
parentNode.insertBefore(node.firstChild, node); | ||
} | ||
|
||
parentNode.removeChild(node); | ||
return parentNode; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages-content-model/roosterjs-content-model-dom/lib/domUtils/wrap.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,19 @@ | ||
/** | ||
* Wrap the given node with a new element, put the wrapper node under the parent of the first node | ||
* and return the wrapper element. | ||
* @param doc Parent document object | ||
* @param node The node to wrap | ||
* @param wrapperTag The tag of wrapper HTML element | ||
* @returns The wrapper element | ||
*/ | ||
export function wrap<T extends keyof HTMLElementTagNameMap>( | ||
doc: Document, | ||
node: Node, | ||
wrapperTag: T | ||
): HTMLElementTagNameMap[T] { | ||
const wrapper = doc.createElement(wrapperTag); | ||
node.parentNode?.insertBefore(wrapper, node); | ||
wrapper.appendChild(node); | ||
|
||
return wrapper; | ||
} |
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: 1 addition & 1 deletion
2
packages-content-model/roosterjs-content-model-dom/lib/modelToDom/handlers/handleListItem.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
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
Oops, something went wrong.