Skip to content
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

BREAKING fix(Text): styles onInput #9096

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dc10071
checkout
ShaMan123 Jul 15, 2023
867246a
Update StyledText.ts
ShaMan123 Jul 15, 2023
639bef4
Update StyledText.ts
ShaMan123 Jul 15, 2023
6e9c7ae
Update ITextKeyBehavior.ts
ShaMan123 Jul 15, 2023
b1e8c75
BERAKING: rm `get2DCursorLocation`
ShaMan123 Jul 15, 2023
1f80e84
restore onInput
ShaMan123 Jul 15, 2023
df3ec23
update CHANGELOG.md
github-actions[bot] Jul 15, 2023
0222e0a
BREAKING: rm unused methods
ShaMan123 Jul 15, 2023
5928fdb
Merge branch 'fix-text-styles!' of https://github.com/fabricjs/fabric…
ShaMan123 Jul 15, 2023
7c8d943
Update CHANGELOG.md
ShaMan123 Jul 15, 2023
7d9f668
goldens
ShaMan123 Jul 15, 2023
a1ca9f3
update CHANGELOG.md
github-actions[bot] Jul 15, 2023
0722748
Squashed commit of the following:
ShaMan123 Jul 15, 2023
c01c5d6
update CHANGELOG.md
github-actions[bot] Jul 15, 2023
46911e4
Merge branch 'fix-text-styles!' of https://github.com/fabricjs/fabric…
ShaMan123 Jul 15, 2023
d596268
almost
ShaMan123 Jul 15, 2023
88cc93c
better
ShaMan123 Jul 15, 2023
92b2552
Update Text.ts
ShaMan123 Jul 15, 2023
996426e
FIX!@#
ShaMan123 Jul 15, 2023
098575b
Update ITextKeyBehavior.ts
ShaMan123 Jul 15, 2023
5be1fde
Update StyledText.ts
ShaMan123 Jul 15, 2023
3709b71
Squashed commit of the following:
ShaMan123 Jul 15, 2023
75d1db3
squash => `getStyleDeclaration`
ShaMan123 Jul 15, 2023
e125566
Update StyledText.ts
ShaMan123 Jul 15, 2023
34ce049
cleanup
ShaMan123 Jul 15, 2023
73e88ec
?? verfiy
ShaMan123 Jul 15, 2023
8906638
Update Text.ts
ShaMan123 Jul 15, 2023
48382e4
Create bug.spec.ts
ShaMan123 Jul 15, 2023
9f30006
tests
ShaMan123 Jul 15, 2023
01e7264
goldens
ShaMan123 Jul 15, 2023
9735e7d
rm dead tests
ShaMan123 Jul 15, 2023
7c42559
Update drag_image.png
ShaMan123 Jul 15, 2023
52a0ee7
Update types.ts
ShaMan123 Jul 15, 2023
c6431f5
extract logic
ShaMan123 Jul 15, 2023
fd08e64
Update InputEventDiff.ts
ShaMan123 Jul 15, 2023
6d2e058
Merge branch 'master' into fix-text-styles!
ShaMan123 Jul 15, 2023
88ceba5
Update StyledText.ts
ShaMan123 Jul 15, 2023
10ca00f
Update Text.spec.ts
ShaMan123 Jul 16, 2023
c2bc332
update CHANGELOG.md
github-actions[bot] Jul 16, 2023
d23aa43
Merge branch 'master' into fix-text-styles!
ShaMan123 Jul 19, 2023
7d199e8
test
ShaMan123 Jul 19, 2023
f972808
resize
ShaMan123 Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [next]

- BREAKING fix(Text): styles `onInput` [#9096](https://github.com/fabricjs/fabric.js/pull/9096)
**BERAKING**: `get2DCursorLocation` => `getCursorPosition`, `getStyleCursorPosition`
**BREAKING**: rm `getCurrentCharFontSize`, `getCurrentCharColor`, `_getCurrentCharIndex`
Comment on lines +6 to +7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to #8299

**BREAKING**: rm `cleanStyle`, `removeStyle`, `_extendStyles`, `_setStyleDeclaration`, `_deleteStyleDeclaration`, `_getLineStyle`, `_setLineStyle`, `_deleteLineStyle`
**BREAKING**: rm `_getStyleDeclaration`, `getCompleteStyleDeclaration` => `getStyleDeclaration`
- fix(): #8344 stroke projection [#8374](https://github.com/fabricjs/fabric.js/pull/8374)
- fix(Filters) Removing type from the options passed in the constructor [#9089](https://github.com/fabricjs/fabric.js/pull/9089)
- feat(InteractiveObject): add `getActiveControl()` to expose `__corner` [#9102](https://github.com/fabricjs/fabric.js/pull/9102)
Expand Down
156 changes: 156 additions & 0 deletions e2e/tests/text/input+styles/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { expect, test } from '@playwright/test';

import '../../../setup';

test('test', async ({ page }) => {
const canvas = page.locator('canvas.upper-canvas');
await canvas.click({
position: {
x: 156,
y: 65,
},
delay: 200,
});
await canvas.click({
position: {
x: 176,
y: 152,
},
delay: 200,
});
await page.mouse.down();
await page.mouse.move(60, 66, { steps: 40 });
await page.mouse.up();
await canvas.press('Control+c');
await canvas.click({
position: {
x: 176,
y: 152,
},
delay: 200,
});
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('Control+v');
await canvas.click({
position: {
x: 246,
y: 371,
},
});
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('x');
await canvas.click({
position: {
x: 144,
y: 202,
},
});
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('ArrowUp');
await canvas.press('ArrowUp');
await canvas.press('Delete');
await canvas.click({
position: {
x: 170,
y: 203,
},
});
await canvas.press('Enter');
await canvas.click({
position: {
x: 136,
y: 336,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 103,
y: 334,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 238,
y: 423,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 52,
y: 480,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 81,
y: 231,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 91,
y: 265,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 182,
y: 158,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 79,
y: 146,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 137,
y: 65,
},
});
await canvas.press('#');
await canvas.click({
position: {
x: 131,
y: 265,
},
});
await expect(await canvas.screenshot()).toMatchSnapshot();
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('#');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Backspace');
await canvas.press('Delete');
await canvas.press('Delete');
await canvas.press('Delete');
await canvas.press('Enter');
await canvas.press('Enter');
await canvas.press('Backspace');
await canvas.press('ArrowUp');
await canvas.press('Delete');
await expect(await canvas.screenshot()).toMatchSnapshot();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions e2e/tests/text/input+styles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as fabric from 'fabric';
import { beforeAll } from 'test';

beforeAll(
(canvas) => {
const textValue = 'fabric.js sandbox';
const text = new fabric.Textbox(textValue, {
originX: 'center',
splitByGrapheme: true,
width: 200,
top: 20,
styles: fabric.util.stylesFromArray(
[
{
style: {
fontWeight: 'bold',
fontSize: 64,
},
start: 0,
end: 9,
},
],
textValue
),
});
canvas.add(text);
canvas.centerObjectH(text);
},
{ width: 300, height: 700 }
);
3 changes: 2 additions & 1 deletion src/env/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { TextStyleDeclaration } from '../../fabric';
import type { GLProbe } from '../filters/GLProbes/GLProbe';
import type { DOMWindow } from 'jsdom';

export type TCopyPasteData = {
copiedText?: string;
copiedStyle?: Record<string, string>;
copiedTextStyle?: TextStyleDeclaration[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asturur this bug was not fixed in your PR, was it?

};
export type TFabricEnv = {
document: Document;
Expand Down
68 changes: 10 additions & 58 deletions src/shapes/IText/IText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
keysMap,
keysMapRtl,
} from './constants';
import type { TFiller } from '../../typedefs';
import { classRegistry } from '../../ClassRegistry';
import type { SerializedTextProps, TextProps } from '../Text/Text';
import {
Expand Down Expand Up @@ -327,18 +326,6 @@ export class IText<
return super.setSelectionStyles(styles, startIndex, endIndex);
}

/**
* Returns 2d representation (lineIndex and charIndex) of cursor (or selection start)
* @param {Number} [selectionStart] Optional index. When not given, current selectionStart is used.
* @param {Boolean} [skipWrapping] consider the location for unwrapped lines. useful to manage styles.
*/
get2DCursorLocation(
selectionStart = this.selectionStart,
skipWrapping?: boolean
) {
return super.get2DCursorLocation(selectionStart, skipWrapping);
}

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
Expand Down Expand Up @@ -435,7 +422,7 @@ export class IText<
__getCursorBoundariesOffsets(index: number) {
let topOffset = 0,
leftOffset = 0;
const { charIndex, lineIndex } = this.get2DCursorLocation(index);
const { charIndex, lineIndex } = this.getCursorPosition(index);

for (let i = 0; i < lineIndex; i++) {
topOffset += this.getHeightOfLine(i);
Expand Down Expand Up @@ -496,14 +483,17 @@ export class IText<
boundaries: CursorBoundaries,
selectionStart: number
) {
const cursorLocation = this.get2DCursorLocation(selectionStart),
const cursorLocation = this.getCursorPosition(selectionStart),
lineIndex = cursorLocation.lineIndex,
charIndex =
cursorLocation.charIndex > 0 ? cursorLocation.charIndex - 1 : 0,
charHeight = this.getValueOfPropertyAt(lineIndex, charIndex, 'fontSize'),
{
fontSize: charHeight,
deltaY: dy,
fill,
} = this.getStyleDeclaration(lineIndex, charIndex, true),
multiplier = this.scaleX * this.canvas!.getZoom(),
cursorWidth = this.cursorWidth / multiplier,
dy = this.getValueOfPropertyAt(lineIndex, charIndex, 'deltaY'),
topOffset =
boundaries.topOffset +
((1 - this._fontSizeFraction) * this.getHeightOfLine(lineIndex)) /
Expand All @@ -515,9 +505,7 @@ export class IText<
// and why can't happen at the top of the function
this.renderSelection(ctx, boundaries);
}
ctx.fillStyle =
this.cursorColor ||
(this.getValueOfPropertyAt(lineIndex, charIndex, 'fill') as string);
ctx.fillStyle = this.cursorColor || (fill as string);
ctx.globalAlpha = this._currentCursorOpacity;
ctx.fillRect(
boundaries.left + boundaries.leftOffset - cursorWidth / 2,
Expand Down Expand Up @@ -577,8 +565,8 @@ export class IText<
const selectionStart = selection.selectionStart,
selectionEnd = selection.selectionEnd,
isJustify = this.textAlign.includes(JUSTIFY),
start = this.get2DCursorLocation(selectionStart),
end = this.get2DCursorLocation(selectionEnd),
start = this.getCursorPosition(selectionStart),
end = this.getCursorPosition(selectionEnd),
startLine = start.lineIndex,
endLine = end.lineIndex,
startChar = start.charIndex < 0 ? 0 : start.charIndex,
Expand Down Expand Up @@ -651,42 +639,6 @@ export class IText<
}
}

/**
* High level function to know the height of the cursor.
* the currentChar is the one that precedes the cursor
* Returns fontSize of char at the current cursor
* Unused from the library, is for the end user
* @return {Number} Character font size
*/
getCurrentCharFontSize(): number {
const cp = this._getCurrentCharIndex();
return this.getValueOfPropertyAt(cp.l, cp.c, 'fontSize');
}

/**
* High level function to know the color of the cursor.
* the currentChar is the one that precedes the cursor
* Returns color (fill) of char at the current cursor
* if the text object has a pattern or gradient for filler, it will return that.
* Unused by the library, is for the end user
* @return {String | TFiller} Character color (fill)
*/
getCurrentCharColor(): string | TFiller | null {
const cp = this._getCurrentCharIndex();
return this.getValueOfPropertyAt(cp.l, cp.c, 'fill');
}

/**
* Returns the cursor position for the getCurrent.. functions
* @private
*/
_getCurrentCharIndex() {
const cursorPosition = this.get2DCursorLocation(this.selectionStart, true),
charIndex =
cursorPosition.charIndex > 0 ? cursorPosition.charIndex - 1 : 0;
return { l: cursorPosition.lineIndex, c: charIndex };
}

dispose() {
this._exitEditing();
this.draggableTextDelegate.dispose();
Expand Down
16 changes: 5 additions & 11 deletions src/shapes/IText/ITextBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,8 @@ export abstract class ITextBehavior<
? this.compositionStart
: this.selectionStart,
boundaries = this._getCursorBoundaries(desiredPosition),
cursorLocation = this.get2DCursorLocation(desiredPosition),
lineIndex = cursorLocation.lineIndex,
charIndex = cursorLocation.charIndex,
charHeight =
this.getValueOfPropertyAt(lineIndex, charIndex, 'fontSize') *
this.lineHeight,
this.getStyleValue(desiredPosition, 'fontSize') * this.lineHeight,
leftOffset = boundaries.leftOffset,
retinaScaling = this.getCanvasRetinaScaling(),
upperCanvas = this.canvas.upperCanvasEl,
Expand Down Expand Up @@ -714,11 +710,9 @@ export abstract class ITextBehavior<
*/
removeStyleFromTo(start: number, end: number) {
const { lineIndex: lineStart, charIndex: charStart } =
this.get2DCursorLocation(start, true),
{ lineIndex: lineEnd, charIndex: charEnd } = this.get2DCursorLocation(
end,
true
);
this.getStyleCursorPosition(start),
{ lineIndex: lineEnd, charIndex: charEnd } =
this.getStyleCursorPosition(end);
if (lineStart !== lineEnd) {
// step1 remove the trailing of lineStart
if (this.styles[lineStart]) {
Expand Down Expand Up @@ -926,7 +920,7 @@ export abstract class ITextBehavior<
start: number,
copiedStyle?: TextStyleDeclaration[]
) {
const cursorLoc = this.get2DCursorLocation(start, true),
const cursorLoc = this.getStyleCursorPosition(start),
addedLines = [0];
let linesLength = 0;
// get an array of how many char per lines are being added.
Expand Down
Loading
Loading