Skip to content

Commit

Permalink
Merge branch 'master' into u/juliaroldi/images-word-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi authored Nov 22, 2024
2 parents a91d593 + 3bbddf5 commit 5a1957f
Show file tree
Hide file tree
Showing 45 changed files with 2,069 additions and 957 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { reducedModelChildProcessor } from '../../modelApi/common/reducedModelChildProcessor';
import { retrieveModelFormatState } from 'roosterjs-content-model-dom';
import type { IEditor, ContentModelFormatState } from 'roosterjs-content-model-types';
import type { IEditor, ContentModelFormatState, ConflictFormatSolution } from 'roosterjs-content-model-types';

/**
* Get current format state
* @param editor The editor to get format from
* @param conflictSolution The strategy for handling format conflicts
*/
export function getFormatState(editor: IEditor): ContentModelFormatState {
export function getFormatState(
editor: IEditor,
conflictSolution: ConflictFormatSolution = 'remove'
): ContentModelFormatState {
const pendingFormat = editor.getPendingFormat();
const manager = editor.getSnapshotsManager();
const result: ContentModelFormatState = {
Expand All @@ -17,7 +21,7 @@ export function getFormatState(editor: IEditor): ContentModelFormatState {

editor.formatContentModel(
model => {
retrieveModelFormatState(model, pendingFormat, result);
retrieveModelFormatState(model, pendingFormat, result, conflictSolution);

return false;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';
import { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-dom';
import { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-content-model-dom';
import type {
ContentModelSegmentFormat,
IEditor,
Expand Down Expand Up @@ -72,12 +72,18 @@ export function formatSegmentWithContentModel(
)
: false;

formatsAndSegments.forEach(([format, segment, paragraph]) =>
toggleStyleCallback(format, !isTurningOff, segment, paragraph)
);
formatsAndSegments.forEach(([format, segment, paragraph]) => {
toggleStyleCallback(format, !isTurningOff, segment, paragraph);
});

afterFormatCallback?.(model);

formatsAndSegments.forEach(([_, __, paragraph]) => {
if (paragraph) {
mergeTextSegments(paragraph);
}
});

if (isCollapsedSelection) {
context.newPendingFormat = segmentAndParagraphs[0][0].format;
editor.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ describe('getFormatState', () => {
canUndo: false,
canRedo: false,
isDarkMode: false,
}
},
'remove'
);
expect(result).toEqual(expectedFormat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,7 @@ describe('applySegmentFormat', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
backgroundColor: undefined,
fontFamily: undefined,
fontSize: '10px',
fontWeight: undefined,
italic: true,
strikethrough: undefined,
superOrSubScriptSequence: undefined,
textColor: 'red',
underline: false,
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
backgroundColor: undefined,
fontFamily: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,7 @@ describe('setBackgroundColor', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
backgroundColor: 'red',
},
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
backgroundColor: 'red',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,7 @@ describe('setFontName', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
fontFamily: 'Arial',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
fontFamily: 'Arial',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,7 @@ describe('setFontSize', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
fontSize: '10pt',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
fontSize: '10pt',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,7 @@ describe('setTextColor', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
textColor: 'red',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
textColor: 'red',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleBold', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
fontWeight: 'bold',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
fontWeight: 'bold',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,7 @@ describe('toggleCode', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
isSelected: true,
code: {
format: {
fontFamily: 'monospace',
},
},
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {},
isSelected: true,
code: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleItalic', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
italic: true,
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
italic: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleStrikethrough', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
strikethrough: true,
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
strikethrough: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleSubscript', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
superOrSubScriptSequence: 'sub',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
superOrSubScriptSequence: 'sub',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleSuperscript', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
superOrSubScriptSequence: 'super',
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
superOrSubScriptSequence: 'super',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ describe('toggleUnderline', () => {
segments: [
{
segmentType: 'Text',
text: 'test',
format: {
underline: true,
},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
text: 'testtest',
format: {
underline: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ function isIndexedSegment(node: Node): node is IndexedSegmentNode {
paragraph &&
paragraph.blockType == 'Paragraph' &&
Array.isArray(paragraph.segments) &&
Array.isArray(segments)
Array.isArray(segments) &&
segments.every(segment => paragraph.segments.includes(segment))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ class CopyPastePlugin implements PluginWithState<CopyPastePluginState> {

const dataTransfer = event.clipboardData;

if (dataTransfer?.items) {
if (shouldPreventDefaultPaste(dataTransfer, editor)) {
event.preventDefault();
extractClipboardItems(
toArray(dataTransfer.items),
toArray(dataTransfer!.items),
this.state.allowedCustomPasteType
).then((clipboardData: ClipboardData) => {
if (!editor.isDisposed()) {
Expand Down Expand Up @@ -339,6 +339,32 @@ export function preprocessTable(table: ContentModelTable) {
: [];
}

/**
* @internal
* Exported only for unit testing
*/
export function shouldPreventDefaultPaste(
dataTransfer: DataTransfer | null,
editor: IEditor
): boolean {
if (!dataTransfer?.items) {
return false;
}

if (!editor.getEnvironment().isAndroid) {
return true;
}

// On Android, the clipboard data from Office apps is a file, which can't be loaded
// so we have to allow the default browser behavior
return toArray(dataTransfer.items).some(item => {
const { type } = item;
const isNormalFile = item.kind === 'file' && type !== '';
const isText = type.indexOf('text/') === 0;
return isNormalFile || isText;
});
}

/**
* @internal
* Create a new instance of CopyPastePlugin
Expand Down
Loading

0 comments on commit 5a1957f

Please sign in to comment.