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

feat(docs): support background color in doc #1846

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/docs-ui/src/controllers/doc-ui.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import {
AlignJustifyMenuItemFactory,
AlignLeftMenuItemFactory,
AlignRightMenuItemFactory,
BackgroundColorSelectorMenuItemFactory,
BoldMenuItemFactory,
BulletListMenuItemFactory,
FontFamilySelectorMenuItemFactory,
FontSizeSelectorMenuItemFactory,
ItalicMenuItemFactory,
OrderListMenuItemFactory,
ResetBackgroundColorMenuItemFactory,
StrikeThroughMenuItemFactory,
SubscriptMenuItemFactory,
SuperscriptMenuItemFactory,
Expand Down Expand Up @@ -92,6 +94,8 @@ export class DocUIController extends Disposable {
AlignJustifyMenuItemFactory,
OrderListMenuItemFactory,
BulletListMenuItemFactory,
ResetBackgroundColorMenuItemFactory,
BackgroundColorSelectorMenuItemFactory,
] as IMenuItemFactory[]
).forEach((factory) => {
this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
Expand Down
47 changes: 47 additions & 0 deletions packages/docs-ui/src/controllers/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
AlignRightCommand,
BulletListCommand,
OrderListCommand,
ResetInlineFormatTextBackgroundColorCommand,
SetInlineFormatBoldCommand,
SetInlineFormatCommand,
SetInlineFormatFontFamilyCommand,
Expand All @@ -39,6 +40,7 @@ import {
SetInlineFormatStrikethroughCommand,
SetInlineFormatSubscriptCommand,
SetInlineFormatSuperscriptCommand,
SetInlineFormatTextBackgroundColorCommand,
SetInlineFormatTextColorCommand,
SetInlineFormatUnderlineCommand,
SetTextSelectionsOperation,
Expand Down Expand Up @@ -564,6 +566,51 @@ export function BulletListMenuItemFactory(accessor: IAccessor): IMenuButtonItem
};
}

export function ResetBackgroundColorMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
id: ResetInlineFormatTextBackgroundColorCommand.id,
type: MenuItemType.BUTTON,
title: 'toolbar.resetColor',
icon: 'NoColor',
positions: SetInlineFormatTextBackgroundColorCommand.id,
};
}

export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
const commandService = accessor.get(ICommandService);
const themeService = accessor.get(ThemeService);

return {
id: SetInlineFormatTextBackgroundColorCommand.id,
tooltip: 'toolbar.fillColor.main',
group: MenuGroup.TOOLBAR_FORMAT,
type: MenuItemType.BUTTON_SELECTOR,
positions: [MenuPosition.TOOLBAR_START],
icon: 'PaintBucket',
selections: [
{
label: {
name: COLOR_PICKER_COMPONENT,
hoverable: false,
},
},
],
value$: new Observable<string>((subscriber) => {
const defaultColor = themeService.getCurrentTheme().primaryColor;
const disposable = commandService.onCommandExecuted((c) => {
if (c.id === SetInlineFormatTextBackgroundColorCommand.id) {
const color = (c.params as { value: string }).value;
subscriber.next(color ?? defaultColor);
}
});

subscriber.next(defaultColor);
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.DOC),
};
}

function getFontStyleAtCursor(accessor: IAccessor) {
const currentUniverService = accessor.get(IUniverInstanceService);
const textSelectionService = accessor.get(TextSelectionManagerService);
Expand Down
4 changes: 4 additions & 0 deletions packages/docs-ui/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const locale: typeof zhCN = {
main: 'Text color',
right: 'Choose color',
},
fillColor: {
main: 'Text Background color',
right: 'Choose color',
},
resetColor: 'Reset',
order: 'Ordered list',
unorder: 'Unordered list',
Expand Down
4 changes: 4 additions & 0 deletions packages/docs-ui/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const locale = {
main: '文本颜色',
right: '颜色选择',
},
fillColor: {
main: '文本背景色',
right: '背景色选择',
},
resetColor: '重置颜色',
order: '有序列表',
unorder: '无序列表',
Expand Down
46 changes: 45 additions & 1 deletion packages/docs/src/commands/commands/inline-format.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,40 @@ export const SetInlineFormatTextColorCommand: ICommand = {
},
};

const SetInlineFormatTextBackgroundColorCommandId = 'doc.command.set-inline-format-text-background-color';
export const SetInlineFormatTextBackgroundColorCommand: ICommand = {
id: SetInlineFormatTextBackgroundColorCommandId,
type: CommandType.COMMAND,
handler: async (accessor, params) => {
const commandService = accessor.get(ICommandService);
const textSelectionManagerService = accessor.get(TextSelectionManagerService);

return handleInlineFormat(
SetInlineFormatTextBackgroundColorCommandId,
params,
textSelectionManagerService,
commandService
);
},
};

const ResetInlineFormatTextBackgroundColorCommandId = 'doc.command.reset-inline-format-text-background-color';
export const ResetInlineFormatTextBackgroundColorCommand: ICommand = {
id: ResetInlineFormatTextBackgroundColorCommandId,
type: CommandType.COMMAND,
handler: async (accessor, params) => {
const commandService = accessor.get(ICommandService);
const textSelectionManagerService = accessor.get(TextSelectionManagerService);

return handleInlineFormat(
ResetInlineFormatTextBackgroundColorCommandId,
params,
textSelectionManagerService,
commandService
);
},
};

const COMMAND_ID_TO_FORMAT_KEY_MAP: Record<string, keyof IStyleBase> = {
[SetInlineFormatBoldCommand.id]: 'bl',
[SetInlineFormatItalicCommand.id]: 'it',
Expand All @@ -220,6 +254,8 @@ const COMMAND_ID_TO_FORMAT_KEY_MAP: Record<string, keyof IStyleBase> = {
[SetInlineFormatFontSizeCommand.id]: 'fs',
[SetInlineFormatFontFamilyCommand.id]: 'ff',
[SetInlineFormatTextColorCommand.id]: 'cl',
[SetInlineFormatTextBackgroundColorCommand.id]: 'bg',
[ResetInlineFormatTextBackgroundColorCommand.id]: 'bg',
[SetInlineFormatSubscriptCommand.id]: 'va',
[SetInlineFormatSuperscriptCommand.id]: 'va',
};
Expand Down Expand Up @@ -273,13 +309,21 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
break;
}

case SetInlineFormatTextColorCommand.id: {
case SetInlineFormatTextColorCommand.id:
case SetInlineFormatTextBackgroundColorCommand.id: {
formatValue = {
rgb: value,
};
break;
}

case ResetInlineFormatTextBackgroundColorCommand.id: {
formatValue = {
rgb: null,
};
break;
}

default: {
throw new Error(`Unknown command: ${preCommandId} in handleInlineFormat`);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/docs/src/doc-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { DeleteCommand, InsertCommand, UpdateCommand } from './commands/commands
import { DeleteLeftCommand, DeleteRightCommand, MergeTwoParagraphCommand } from './commands/commands/delete.command';
import { IMEInputCommand } from './commands/commands/ime-input.command';
import {
ResetInlineFormatTextBackgroundColorCommand,
SetInlineFormatBoldCommand,
SetInlineFormatCommand,
SetInlineFormatFontFamilyCommand,
Expand All @@ -36,6 +37,7 @@ import {
SetInlineFormatStrikethroughCommand,
SetInlineFormatSubscriptCommand,
SetInlineFormatSuperscriptCommand,
SetInlineFormatTextBackgroundColorCommand,
SetInlineFormatTextColorCommand,
SetInlineFormatUnderlineCommand,
} from './commands/commands/inline-format.command';
Expand Down Expand Up @@ -103,6 +105,8 @@ export class UniverDocsPlugin extends Plugin {
SetInlineFormatFontSizeCommand,
SetInlineFormatFontFamilyCommand,
SetInlineFormatTextColorCommand,
ResetInlineFormatTextBackgroundColorCommand,
SetInlineFormatTextBackgroundColorCommand,
SetInlineFormatCommand,
BreakLineCommand,
InsertCommand,
Expand Down
2 changes: 2 additions & 0 deletions packages/docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export {
SetInlineFormatSuperscriptCommand,
SetInlineFormatTextColorCommand,
SetInlineFormatUnderlineCommand,
ResetInlineFormatTextBackgroundColorCommand,
SetInlineFormatTextBackgroundColorCommand,
} from './commands/commands/inline-format.command';
export { AlignOperationCommand, AlignLeftCommand, AlignCenterCommand, AlignRightCommand, AlignJustifyCommand } from './commands/commands/paragraph-align.command';
export { BulletListCommand, OrderListCommand } from './commands/commands/list.command';
Expand Down
Loading