-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add / remove declaration in preview (#11)
* refactor: DeclarationUpdater -> DeclarationForUpdate * chore: add `before` and `after` property in style node * chore: add a helper to remove declaration * chore: modify declaration to insert or remove * feat: remove declaration from the code when its value is updated to empty in preview * feat: add declaration when any area of rule element is clicked
- Loading branch information
Showing
18 changed files
with
392 additions
and
53 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
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,18 +1,80 @@ | ||
import { Style, DeclarationUpdater } from './types' | ||
import { getDeclaration } from './manipulate' | ||
import { Modifier, empty, replace } from '../modifier' | ||
import { genDeclaration } from './codegen' | ||
import { | ||
Style, | ||
DeclarationForUpdate, | ||
DeclarationForAdd, | ||
Declaration | ||
} from './types' | ||
import { getDeclaration, getNode } from './manipulate' | ||
import { | ||
Modifier, | ||
empty, | ||
replace, | ||
removeRange, | ||
insertBefore, | ||
insertAfter | ||
} from '../modifier' | ||
import { genDeclaration, genRule } from './codegen' | ||
import { clone } from '../../utils' | ||
|
||
export function updateDeclaration( | ||
export function insertDeclaration( | ||
styles: Style[], | ||
decl: DeclarationUpdater | ||
): Modifier[] { | ||
const target = getDeclaration(styles, decl.path) | ||
decl: DeclarationForAdd, | ||
to: number[] | ||
): Modifier | Modifier[] { | ||
const target = getDeclaration(styles, to) | ||
if (target) { | ||
return insertBefore( | ||
target, | ||
genDeclaration(clone(target, decl)) + target.before | ||
) | ||
} | ||
|
||
const parentPath = to.slice(0, -1) | ||
const last = to[to.length - 1] | ||
const before = getDeclaration(styles, parentPath.concat(last - 1)) | ||
if (before) { | ||
return insertAfter( | ||
before, | ||
before.before + genDeclaration(clone(before, decl)) | ||
) | ||
} | ||
|
||
const rule = getNode(styles, parentPath) | ||
if (rule && rule.type === 'Rule') { | ||
const d: Declaration = { | ||
type: 'Declaration', | ||
path: to, | ||
before: '', | ||
after: '', | ||
range: [-1, -1], | ||
...decl | ||
} | ||
|
||
const inserted = clone(rule, { | ||
declarations: [ | ||
...rule.declarations.slice(last), | ||
d, | ||
...rule.declarations.slice(last + 1) | ||
] | ||
}) | ||
|
||
if (!target) { | ||
return [empty] | ||
return replace(rule, genRule(inserted)) | ||
} | ||
|
||
return replace(target, genDeclaration(clone(target, decl))) | ||
return empty | ||
} | ||
|
||
export function removeDeclaration(styles: Style[], path: number[]): Modifier { | ||
const target = getDeclaration(styles, path) | ||
return target | ||
? removeRange(target.range[0] - target.before.length, target.range[1]) | ||
: empty | ||
} | ||
|
||
export function updateDeclaration( | ||
styles: Style[], | ||
decl: DeclarationForUpdate | ||
): Modifier[] { | ||
const target = getDeclaration(styles, decl.path) | ||
return target ? replace(target, genDeclaration(clone(target, decl))) : [empty] | ||
} |
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
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.