Skip to content

Commit 2673c1b

Browse files
committed
Merge branch 'u/juliaroldi/preserve-cursor-postion-yab' of https://github.com/microsoft/roosterjs into u/juliaroldi/preserve-cursor-postion-yab
2 parents be03266 + 45d385a commit 2673c1b

20 files changed

Lines changed: 1167 additions & 343 deletions

File tree

karma.fast.conf.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const argv = require('minimist')(process.argv.slice(2));
22
const components = argv.components !== true && argv.components;
3+
const testPathPattern = argv.testPathPattern !== true && argv.testPathPattern;
4+
const testNamePattern = argv.testNamePattern !== true && argv.testNamePattern;
35
const runCoverage = typeof argv.coverage !== 'undefined';
46
const runFirefox = typeof argv.firefox !== 'undefined';
57
const runChrome = typeof argv.chrome !== 'undefined';
@@ -69,8 +71,13 @@ module.exports = function (config) {
6971
plugins,
7072
client: {
7173
components: components,
74+
testPathPattern: testPathPattern,
75+
testNamePattern: testNamePattern,
7276
clearContext: false,
7377
captureConsole: true,
78+
jasmine: {
79+
grep: testNamePattern || null,
80+
},
7481
},
7582
browsers: launcher,
7683
files: ['tools/karma.test.all.js'],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"typedoc-plugin-remove-references": "0.0.5",
8484
"typescript": "4.4.4",
8585
"url-loader": "4.1.0",
86-
"webpack": "5.94.0",
86+
"webpack": "5.104.1",
8787
"webpack-cli": "3.3.11",
8888
"webpack-dev-server": "3.10.3"
8989
},

packages/roosterjs-content-model-core/lib/editor/Editor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ export class Editor implements IEditor {
310310
core.darkColorHandler,
311311
{
312312
tableBorders: this.isExperimentalFeatureEnabled('TransformTableBorderColors'),
313-
}
313+
},
314+
core.format.defaultFormat.textColor
314315
);
315316

316317
core.lifecycle.isDarkMode = !!isDarkMode;

packages/roosterjs-content-model-core/test/editor/EditorTest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,8 @@ describe('Editor', () => {
10161016
mockedColorHandler,
10171017
{
10181018
tableBorders: false,
1019-
}
1019+
},
1020+
undefined
10201021
);
10211022
expect(mockedCore.lifecycle.isDarkMode).toEqual(true);
10221023
expect(triggerEventSpy).toHaveBeenCalledTimes(1);
@@ -1041,7 +1042,8 @@ describe('Editor', () => {
10411042
mockedColorHandler,
10421043
{
10431044
tableBorders: false,
1044-
}
1045+
},
1046+
undefined
10451047
);
10461048
expect(triggerEventSpy).toHaveBeenCalledTimes(2);
10471049
expect(triggerEventSpy).toHaveBeenCalledWith(

packages/roosterjs-content-model-dom/lib/domUtils/style/transformColor.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,34 @@ export function transformColor(
3030
includeSelf: boolean,
3131
direction: 'lightToDark' | 'darkToLight',
3232
darkColorHandler?: DarkColorHandler,
33-
transformColorOptions?: TransformColorOptions
33+
transformColorOptions?: TransformColorOptions,
34+
defaultTextColor?: string
3435
) {
3536
const toDarkMode = direction == 'lightToDark';
3637
const tableBorders = transformColorOptions?.tableBorders || false;
37-
const transformer = (element: HTMLElement) => {
38+
const transformer = (element: HTMLElement, parentTextColor?: string) => {
3839
const textColor = getColor(element, false /*isBackground*/, !toDarkMode, darkColorHandler);
3940
const backColor = getColor(element, true /*isBackground*/, !toDarkMode, darkColorHandler);
41+
const comparingColor = textColor || parentTextColor;
4042

4143
setColor(element, textColor, false /*isBackground*/, toDarkMode, darkColorHandler);
42-
setColor(element, backColor, true /*isBackground*/, toDarkMode, darkColorHandler);
44+
setColor(
45+
element,
46+
backColor,
47+
true /*isBackground*/,
48+
toDarkMode,
49+
darkColorHandler,
50+
comparingColor
51+
);
4352

4453
if (tableBorders) {
4554
transformBorderColor(element, toDarkMode, darkColorHandler);
4655
}
56+
57+
return comparingColor;
4758
};
4859

49-
iterateElements(rootNode, transformer, includeSelf);
60+
iterateElements(rootNode, transformer, includeSelf, defaultTextColor);
5061
}
5162

5263
function transformBorderColor(
@@ -79,19 +90,22 @@ function transformBorderColor(
7990

8091
function iterateElements(
8192
root: Node,
82-
transformer: (element: HTMLElement) => void,
83-
includeSelf?: boolean
93+
transformer: (element: HTMLElement, parentTextColor?: string) => string | undefined,
94+
includeSelf?: boolean,
95+
parentTextColor?: string
8496
) {
8597
if (includeSelf && isHTMLElement(root)) {
86-
transformer(root);
98+
parentTextColor = transformer(root, parentTextColor);
8799
}
88100

89101
for (let child = root.firstChild; child; child = child.nextSibling) {
102+
let textColor = parentTextColor;
103+
90104
if (isHTMLElement(child)) {
91-
transformer(child);
105+
textColor = transformer(child, parentTextColor);
92106
}
93107

94-
iterateElements(child, transformer);
108+
iterateElements(child, transformer, false, textColor);
95109
}
96110
}
97111

packages/roosterjs-content-model-dom/lib/formatHandlers/common/backgroundColorFormatHandler.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { getColor, setColor } from '../utils/color';
22
import { shouldSetValue } from '../utils/shouldSetValue';
3-
import type { BackgroundColorFormat } from 'roosterjs-content-model-types';
3+
import type { BackgroundColorFormat, TextColorFormat } from 'roosterjs-content-model-types';
44
import type { FormatHandler } from '../FormatHandler';
55

66
/**
77
* @internal
88
*/
9-
export const backgroundColorFormatHandler: FormatHandler<BackgroundColorFormat> = {
9+
export const backgroundColorFormatHandler: FormatHandler<
10+
BackgroundColorFormat & TextColorFormat
11+
> = {
1012
parse: (format, element, context, defaultStyle) => {
1113
const backgroundColor =
1214
getColor(
@@ -34,7 +36,8 @@ export const backgroundColorFormatHandler: FormatHandler<BackgroundColorFormat>
3436
format.backgroundColor,
3537
true /*isBackground*/,
3638
!!context.isDarkMode,
37-
context.darkColorHandler
39+
context.darkColorHandler,
40+
format.textColor
3841
);
3942
}
4043
},

packages/roosterjs-content-model-dom/lib/formatHandlers/utils/color.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ export function getLightModeColor(
8484
) {
8585
if (DeprecatedColors.indexOf(color) > -1) {
8686
return fallback;
87-
} else if (darkColorHandler) {
87+
} else {
8888
const match = color.startsWith(VARIABLE_PREFIX) ? VARIABLE_REGEX.exec(color) : null;
8989

9090
if (match) {
9191
color = match[2] || '';
92-
} else if (isDarkMode) {
92+
} else if (isDarkMode && darkColorHandler) {
9393
// If editor is in dark mode but the color is not in dark color format, it is possible the color was inserted from external code
9494
// without any light color info. So we first try to see if there is a known dark color can match this color, and use its related
9595
// light color as light mode color. Otherwise we need to drop this color to avoid show "white on white" content.
@@ -126,20 +126,23 @@ export function retrieveElementColor(
126126
* @param isBackground True to set background color, false to set text color
127127
* @param isDarkMode Whether element is in dark mode now
128128
* @param darkColorHandler @optional The dark color handler object to help manager dark mode color
129+
* @param comparingColor @optional When generating dark color for background color, we can provide text color as comparingColor to make sure the generated dark border color has enough contrast with text color in dark mode
129130
*/
130131
export function setColor(
131132
element: HTMLElement,
132133
color: string | null | undefined,
133134
isBackground: boolean,
134135
isDarkMode: boolean,
135-
darkColorHandler?: DarkColorHandler
136+
darkColorHandler?: DarkColorHandler,
137+
comparingColor?: string
136138
) {
137139
const newColor = adaptColor(
138140
element,
139141
color,
140142
isBackground ? 'background' : 'text',
141143
isDarkMode,
142-
darkColorHandler
144+
darkColorHandler,
145+
comparingColor
143146
);
144147

145148
element.removeAttribute(isBackground ? 'bgcolor' : 'color');
@@ -154,7 +157,8 @@ export function adaptColor(
154157
color: string | null | undefined,
155158
colorType: 'text' | 'background' | 'border',
156159
isDarkMode: boolean,
157-
darkColorHandler?: DarkColorHandler
160+
darkColorHandler?: DarkColorHandler,
161+
comparingColor?: string
158162
) {
159163
const match = color && color.startsWith(VARIABLE_PREFIX) ? VARIABLE_REGEX.exec(color) : null;
160164
const [_, existingKey, fallbackColor] = match ?? [];
@@ -164,10 +168,22 @@ export function adaptColor(
164168
if (darkColorHandler && color) {
165169
const key =
166170
existingKey ||
167-
darkColorHandler.generateColorKey(color, undefined /*baseLValue*/, colorType, element);
171+
darkColorHandler.generateColorKey(
172+
color,
173+
undefined /*baseLValue*/,
174+
colorType,
175+
element,
176+
comparingColor
177+
);
168178
const darkModeColor =
169179
darkColorHandler.knownColors?.[key]?.darkModeColor ||
170-
darkColorHandler.getDarkColor(color, undefined /*baseLValue*/, colorType, element);
180+
darkColorHandler.getDarkColor(
181+
color,
182+
undefined /*baseLValue*/,
183+
colorType,
184+
element,
185+
comparingColor
186+
);
171187

172188
darkColorHandler.updateKnownColor(isDarkMode, key, {
173189
lightModeColor: color,
@@ -185,8 +201,15 @@ export function adaptColor(
185201
* @param lightColor The input light color
186202
* @returns Key of the color
187203
*/
188-
export const defaultGenerateColorKey: ColorTransformFunction = lightColor => {
189-
return `${COLOR_VAR_PREFIX}_${lightColor.replace(/[^\d\w]/g, '_')}`;
204+
export const defaultGenerateColorKey: ColorTransformFunction = (
205+
lightColor,
206+
_1,
207+
_2,
208+
_3,
209+
comparingColor
210+
) => {
211+
const comparingColorKey = comparingColor ? `_${comparingColor.replace(/[^\d\w]/g, '_')}` : '';
212+
return `${COLOR_VAR_PREFIX}_${lightColor.replace(/[^\d\w]/g, '_')}${comparingColorKey}`;
190213
};
191214

192215
/**

0 commit comments

Comments
 (0)