-
Notifications
You must be signed in to change notification settings - Fork 66
feat: cropped images #1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VladaHarbour
wants to merge
2
commits into
main
Choose a base branch
from
sd-584_cropped-images
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: cropped images #1940
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/layout-engine/contracts/src/clip-path-inset.test.ts
This file contains hidden or 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,82 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { parseInsetClipPathForScale, formatInsetClipPathTransform } from './clip-path-inset.js'; | ||
|
|
||
| describe('parseInsetClipPathForScale', () => { | ||
| it('returns scale and translate for valid inset(top right bottom left)', () => { | ||
| const result = parseInsetClipPathForScale('inset(10% 20% 30% 40%)'); | ||
| expect(result).not.toBeNull(); | ||
| // visibleW = 100 - 40 - 20 = 40, visibleH = 100 - 10 - 30 = 60 | ||
| // scaleX = 100/40 = 2.5, scaleY = 100/60 = 5/3 | ||
| // translateX = -40*2.5 = -100, translateY = -10*(5/3) = -50/3 | ||
| expect(result!.scaleX).toBeCloseTo(2.5); | ||
| expect(result!.scaleY).toBeCloseTo(100 / 60); | ||
| expect(result!.translateX).toBeCloseTo(-100); | ||
| expect(result!.translateY).toBeCloseTo(-50 / 3); | ||
| }); | ||
|
|
||
| it('returns scale 1 and translate 0 when no inset (full image visible)', () => { | ||
| const result = parseInsetClipPathForScale('inset(0% 0% 0% 0%)'); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.scaleX).toBe(1); | ||
| expect(result!.scaleY).toBe(1); | ||
| expect(result!.translateX).toBeCloseTo(0, 10); | ||
| expect(result!.translateY).toBeCloseTo(0, 10); | ||
| }); | ||
|
|
||
| it('trims whitespace around clipPath', () => { | ||
| const result = parseInsetClipPathForScale(' inset(5% 10% 15% 20%) '); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.scaleX).toBeCloseTo(100 / (100 - 20 - 10)); | ||
| expect(result!.scaleY).toBeCloseTo(100 / (100 - 5 - 15)); | ||
| }); | ||
|
|
||
| it('returns null for non-inset clipPath', () => { | ||
| expect(parseInsetClipPathForScale('circle(50%)')).toBeNull(); | ||
| expect(parseInsetClipPathForScale('polygon(0 0, 100% 0, 100% 100%)')).toBeNull(); | ||
| expect(parseInsetClipPathForScale('')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null for malformed inset', () => { | ||
| expect(parseInsetClipPathForScale('inset(10 20 30 40)')).toBeNull(); // no % | ||
| expect(parseInsetClipPathForScale('inset(10% 20% 30%)')).toBeNull(); // only 3 values | ||
| expect(parseInsetClipPathForScale('inset()')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null when visible area has zero or negative size', () => { | ||
| // left + right >= 100 => visibleW <= 0 | ||
| expect(parseInsetClipPathForScale('inset(0% 50% 0% 50%)')).toBeNull(); | ||
| // top + bottom >= 100 => visibleH <= 0 | ||
| expect(parseInsetClipPathForScale('inset(50% 0% 50% 0%)')).toBeNull(); | ||
| }); | ||
|
|
||
| it('handles decimal percentages', () => { | ||
| const result = parseInsetClipPathForScale('inset(12.5% 25.5% 12.5% 24.5%)'); | ||
| expect(result).not.toBeNull(); | ||
| const visibleW = 100 - 24.5 - 25.5; | ||
| const visibleH = 100 - 12.5 - 12.5; | ||
| expect(result!.scaleX).toBeCloseTo(100 / visibleW); | ||
| expect(result!.scaleY).toBeCloseTo(100 / visibleH); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatInsetClipPathTransform', () => { | ||
| it('returns CSS transform string for valid inset', () => { | ||
| const result = formatInsetClipPathTransform('inset(10% 20% 30% 40%)'); | ||
| expect(result).toBeDefined(); | ||
| expect(result).toContain('transform-origin: 0 0'); | ||
| expect(result).toContain('transform: translate('); | ||
| expect(result).toContain('%) scale('); | ||
| expect(result).toMatch(/translate\([-\d.]+%,\s*[-\d.]+%\)/); | ||
| expect(result).toMatch(/scale\([-\d.]+,\s*[-\d.]+\)/); | ||
| }); | ||
|
|
||
| it('returns undefined for invalid clipPath', () => { | ||
| expect(formatInsetClipPathTransform('circle(50%)')).toBeUndefined(); | ||
| expect(formatInsetClipPathTransform('')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('output can be applied as inline style', () => { | ||
| const result = formatInsetClipPathTransform('inset(0% 0% 0% 0%)'); | ||
| expect(result).toBe('transform-origin: 0 0; transform: translate(0%, 0%) scale(1, 1);'); | ||
| }); | ||
| }); |
This file contains hidden or 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 @@ | ||
| /** | ||
| * Shared utilities for inset(top% right% bottom% left%) clip-path (e.g. from DOCX a:srcRect). | ||
| * Used by both the layout-engine painters and super-editor image extension so the same | ||
| * scale/translate math is applied everywhere. | ||
| */ | ||
|
|
||
| /** Result of parsing an inset() clip-path for scale/translate. */ | ||
| export type InsetClipPathScale = { | ||
| scaleX: number; | ||
| scaleY: number; | ||
| translateX: number; | ||
| translateY: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Parses inset(top% right% bottom% left%) from a clipPath string and returns scale + translate | ||
| * so the visible clipped portion fills the container and is aligned to top-left. | ||
| * | ||
| * @param clipPath - e.g. "inset(10% 20% 30% 40%)" | ||
| * @returns Scale and translate values, or null if not a valid inset() | ||
| */ | ||
| export function parseInsetClipPathForScale(clipPath: string): InsetClipPathScale | null { | ||
| const m = clipPath.trim().match(/^inset\(\s*([\d.]+)%\s+([\d.]+)%\s+([\d.]+)%\s+([\d.]+)%\s*\)$/); | ||
| if (!m) return null; | ||
| const top = Number(m[1]); | ||
| const right = Number(m[2]); | ||
| const bottom = Number(m[3]); | ||
| const left = Number(m[4]); | ||
| const visibleW = 100 - left - right; | ||
| const visibleH = 100 - top - bottom; | ||
| if (visibleW <= 0 || visibleH <= 0) return null; | ||
| const scaleX = 100 / visibleW; | ||
| const scaleY = 100 / visibleH; | ||
| const translateX = -left * scaleX; | ||
| const translateY = -top * scaleY; | ||
| return { scaleX, scaleY, translateX, translateY }; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the CSS transform-origin and transform string from a parsed inset scale result. | ||
| * | ||
| * @param clipPath - e.g. "inset(10% 20% 30% 40%)" | ||
| * @returns CSS fragment: "transform-origin: 0 0; transform: translate(...) scale(...);" | ||
| */ | ||
| export function formatInsetClipPathTransform(clipPath: string): string | undefined { | ||
| const scale = parseInsetClipPathForScale(clipPath); | ||
| if (!scale) return undefined; | ||
| return `transform-origin: 0 0; transform: translate(${scale.translateX}%, ${scale.translateY}%) scale(${scale.scaleX}, ${scale.scaleY});`; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seven individual
img.style.x = ...lines for static values --Object.assign(img.style, { width: '100%', height: '100%', ... })would be more compact, or a CSS class since none of these change.