-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Refactor gradientPatchAdapter (#589)
- Loading branch information
Showing
7 changed files
with
145 additions
and
124 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
33 changes: 33 additions & 0 deletions
33
packages/@sanity/form-builder/src/sanity/utils/convertPath.js
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,33 @@ | ||
// @flow | ||
import {arrayToJSONMatchPath} from '@sanity/mutator' | ||
import type {Path, PathSegment} from '../../typedefs/path' | ||
|
||
const IS_NUMERIC = /^\d+$/ | ||
|
||
function unquote(str) { | ||
return str.replace(/^['"]/, '').replace(/['"]$/, '') | ||
} | ||
|
||
function splitAttr(segment) { | ||
const [attr, key] = segment.split('==') | ||
return {[attr]: unquote(key)} | ||
} | ||
|
||
function coerce(segment: string): PathSegment { | ||
return IS_NUMERIC.test(segment) ? Number(segment) : segment | ||
} | ||
|
||
function parseGradientPath(focusPathStr): Path { | ||
return focusPathStr | ||
.split(/[[.\]]/g) | ||
.filter(Boolean) | ||
.map(seg => (seg.includes('==') ? splitAttr(seg) : coerce(seg))) | ||
} | ||
|
||
export function toGradient(formBuilderPath: Path): string { | ||
return arrayToJSONMatchPath(formBuilderPath) | ||
} | ||
|
||
export function toFormBuilder(gradientPath: string) { | ||
return parseGradientPath(gradientPath) | ||
} |
106 changes: 53 additions & 53 deletions
106
packages/@sanity/form-builder/src/sanity/utils/gradientPatchAdapter.js
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,42 @@ | ||
// @flow | ||
type KeyedSegment = { | ||
_key: string | ||
} | ||
|
||
type JSONValue = number | string | boolean | {[string]: JSONValue} | JSONValue[] | ||
|
||
export type PathSegment = string | number | KeyedSegment | ||
|
||
export type Path = Array<PathSegment> | ||
|
||
export type Origin = 'remote' | 'local' | ||
|
||
type SetPatch = { | ||
path: Path, | ||
type: 'set', | ||
origin: Origin, | ||
value: JSONValue | ||
} | ||
|
||
type SetIfMissingPatch = { | ||
path: Path, | ||
origin: Origin, | ||
type: 'setIfMissing', | ||
value: JSONValue | ||
} | ||
|
||
type UnsetPatch = { | ||
path: Path, | ||
origin: Origin, | ||
type: 'unset' | ||
} | ||
|
||
type InsertPatch = { | ||
path: Path, | ||
origin: Origin, | ||
type: 'insert', | ||
position: 'before' | 'after', | ||
items: JSONValue[] | ||
} | ||
|
||
export type Patch = SetPatch | SetIfMissingPatch | UnsetPatch | InsertPatch |
This file was deleted.
Oops, something went wrong.
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