Skip to content

Commit

Permalink
fix: menu show
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Nov 8, 2024
1 parent 2f1a9cd commit d8b9bc9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
43 changes: 24 additions & 19 deletions packages/docs-ui/src/commands/commands/inline-format.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
const docMenuStyleService = accessor.get(DocMenuStyleService);

const docRanges = docSelectionManagerService.getDocRanges();
const activeTextRange = docSelectionManagerService.getActiveTextRange();

if (docRanges.length === 0) {
if (docRanges.length === 0 || activeTextRange == null) {
return false;
}

Expand All @@ -268,7 +269,7 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
formatValue = getReverseFormatValueInSelection(
docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()!.textRuns!,
preCommandId,
docRanges
activeTextRange
);

break;
Expand Down Expand Up @@ -431,30 +432,34 @@ function getReverseFormatValue(ts: Nullable<ITextStyle>, key: keyof IStyleBase,
function getReverseFormatValueInSelection(
textRuns: ITextRun[],
preCommandId: string,
docRanges: ITextRangeWithStyle[]
activeTextRange: ITextRangeWithStyle
): BooleanNumber | ITextDecoration | BaselineOffset {
let ti = 0;
let si = 0;
const key: keyof IStyleBase = COMMAND_ID_TO_FORMAT_KEY_MAP[preCommandId];
const { startOffset, endOffset, collapsed } = activeTextRange;

while (ti !== textRuns.length && si !== docRanges.length) {
const { startOffset, endOffset } = docRanges[si];
let textRun;

// TODO: @jocs handle sid in textRun
const { st, ed, ts } = textRuns[ti];

if (endOffset! <= st) {
si++;
} else if (ed <= startOffset!) {
ti++;
for (let i = textRuns.length - 1; i >= 0; i--) {
const curTextRun = textRuns[i];
if (collapsed) {
if (curTextRun.st < startOffset && startOffset <= curTextRun.ed) {
textRun = curTextRun;
break;
}
} else {
const reverseValue = getReverseFormatValue(ts, key, preCommandId);

if (reverseValue !== undefined) {
return reverseValue;
if (curTextRun.st <= startOffset && endOffset <= curTextRun.ed) {
textRun = curTextRun;
break;
}
}
}

if (textRun) {
const { ts } = textRun;
const reverseValue = getReverseFormatValue(ts, key, preCommandId);

ti++;
if (reverseValue !== undefined) {
return reverseValue;
}
}

Expand Down
16 changes: 11 additions & 5 deletions packages/docs-ui/src/controllers/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ function getFontStyleAtCursor(accessor: IAccessor) {
};
}

const { startOffset, segmentId } = activeTextRange;
const { startOffset, endOffset, collapsed, segmentId } = activeTextRange;

const textRuns = docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody()?.textRuns;

Expand All @@ -956,10 +956,16 @@ function getFontStyleAtCursor(accessor: IAccessor) {

for (let i = textRuns.length - 1; i >= 0; i--) {
const curTextRun = textRuns[i];

if (curTextRun.st < startOffset && startOffset <= curTextRun.ed) {
textRun = curTextRun;
break;
if (collapsed) {
if (curTextRun.st < startOffset && startOffset <= curTextRun.ed) {
textRun = curTextRun;
break;
}
} else {
if (curTextRun.st <= startOffset && endOffset <= curTextRun.ed) {
textRun = curTextRun;
break;
}
}
}

Expand Down

0 comments on commit d8b9bc9

Please sign in to comment.