Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat: doc support subscript and superscript (#736)
Ran Luod0f2b36c477f
diff --git a/examples/data/src/docs/default-document-data-cn.ts b/examples/data/src/docs/default-document-data-cn.tsindex ce447d4ea7..eff7175f41 100644--- a/examples/data/src/docs/default-document-data-cn.ts+++ b/examples/data/src/docs/default-document-data-cn.ts@@ -15,7 +15,7 @@*/import type { IDocumentData } from '@univerjs/core';-import { BooleanNumber, TextDecoration } from '@univerjs/core';+import { BaselineOffset, BooleanNumber, TextDecoration } from '@univerjs/core';import { DEFAULT_LIST_TEST } from './default-list';@@ -103,7 +103,7 @@ export const DEFAULT_DOCUMENT_DATA_CN: IDocumentData = {cl: {rgb: 'rgb(47, 85, 151)',},- va: 1,+ va: BaselineOffset.SUBSCRIPT,},},{@@ -126,7 +126,7 @@ export const DEFAULT_DOCUMENT_DATA_CN: IDocumentData = {cl: {rgb: 'rgb(47, 85, 151)',},- va: 2,+ va: BaselineOffset.SUPERSCRIPT,},},{diff --git a/packages/core/src/types/enum/text-style.ts b/packages/core/src/types/enum/text-style.tsindex dec0d8182c..ae9d8be486 100644--- a/packages/core/src/types/enum/text-style.ts+++ b/packages/core/src/types/enum/text-style.ts@@ -117,7 +117,7 @@ export enum FontWeight {}export enum BaselineOffset {- NORMAL,+ NORMAL = 1,SUBSCRIPT,SUPERSCRIPT,}diff --git a/packages/docs-ui/src/controllers/doc-ui.controller.ts b/packages/docs-ui/src/controllers/doc-ui.controller.tsindex 1f46ab181e..e8ac40acfb 100644--- a/packages/docs-ui/src/controllers/doc-ui.controller.ts+++ b/packages/docs-ui/src/controllers/doc-ui.controller.ts@@ -23,6 +23,8 @@ import {BoldMenuItemFactory,ItalicMenuItemFactory,StrikeThroughMenuItemFactory,+ SubscriptMenuItemFactory,+ SuperscriptMenuItemFactory,UnderlineMenuItemFactory,} from './menu/menu';@@ -50,6 +52,8 @@ export class DocUIController extends Disposable {ItalicMenuItemFactory,UnderlineMenuItemFactory,StrikeThroughMenuItemFactory,+ SubscriptMenuItemFactory,+ SuperscriptMenuItemFactory,] as IMenuItemFactory[]).forEach((factory) => {this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.tsindex cbc54c3138..8b5bfab04d 100644--- a/packages/docs-ui/src/controllers/menu/menu.ts+++ b/packages/docs-ui/src/controllers/menu/menu.ts@@ -18,6 +18,8 @@ import {SetInlineFormatBoldCommand,SetInlineFormatItalicCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatUnderlineCommand,} from '@univerjs/docs';import type { IMenuButtonItem } from '@univerjs/ui';@@ -68,3 +70,23 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonItpositions: [MenuPosition.TOOLBAR_START],};}++export function SubscriptMenuItemFactory(accessor: IAccessor): IMenuButtonItem {+ return {+ id: SetInlineFormatSubscriptCommand.id,+ type: MenuItemType.BUTTON,+ icon: 'FontSizeReduceSingleSingle',+ tooltip: 'toolbar.subscript',+ positions: [MenuPosition.TOOLBAR_START],+ };+}++export function SuperscriptMenuItemFactory(accessor: IAccessor): IMenuButtonItem {+ return {+ id: SetInlineFormatSuperscriptCommand.id,+ type: MenuItemType.BUTTON,+ icon: 'FontSizeIncreaseSingle',+ tooltip: 'toolbar.superscript',+ positions: [MenuPosition.TOOLBAR_START],+ };+}diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.tsindex c4cf02c5f3..eaf1db8edc 100644--- a/packages/docs/src/commands/commands/inline-format.command.ts+++ b/packages/docs/src/commands/commands/inline-format.command.ts@@ -16,6 +16,7 @@import type { ICommand, IDocumentBody, IMutationInfo, IStyleBase, ITextDecoration, ITextRun } from '@univerjs/core';import {+ BaselineOffset,BooleanNumber,CommandType,DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,@@ -61,6 +62,18 @@ export const SetInlineFormatStrikethroughCommand: ICommand = {handler: async () => true,};+export const SetInlineFormatSubscriptCommand: ICommand = {+ id: 'doc.command.set-inline-format-subscript',+ type: CommandType.COMMAND,+ handler: async () => true,+};++export const SetInlineFormatSuperscriptCommand: ICommand = {+ id: 'doc.command.set-inline-format-superscript',+ type: CommandType.COMMAND,+ handler: async () => true,+};+export const SetInlineFormatFontSizeCommand: ICommand = {id: 'doc.command.set-inline-format-fontsize',type: CommandType.COMMAND,@@ -79,6 +92,18 @@ export const SetInlineFormatTextColorCommand: ICommand = {handler: async () => true,};+const COMMAND_ID_TO_FORMAT_KEY_MAP: Record<string, keyof IStyleBase> = {+ [SetInlineFormatBoldCommand.id]: 'bl',+ [SetInlineFormatItalicCommand.id]: 'it',+ [SetInlineFormatUnderlineCommand.id]: 'ul',+ [SetInlineFormatStrikethroughCommand.id]: 'st',+ [SetInlineFormatFontSizeCommand.id]: 'fs',+ [SetInlineFormatFontFamilyCommand.id]: 'ff',+ [SetInlineFormatTextColorCommand.id]: 'cl',+ [SetInlineFormatSubscriptCommand.id]: 'va',+ [SetInlineFormatSuperscriptCommand.id]: 'va',+};+export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {id: 'doc.command.set-inline-format',type: CommandType.COMMAND,@@ -107,24 +132,16 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {let formatValue;- const COMMAND_ID_TO_FORMAT_KEY_MAP: Record<string, keyof IStyleBase> = {- [SetInlineFormatBoldCommand.id]: 'bl',- [SetInlineFormatItalicCommand.id]: 'it',- [SetInlineFormatUnderlineCommand.id]: 'ul',- [SetInlineFormatStrikethroughCommand.id]: 'st',- [SetInlineFormatFontSizeCommand.id]: 'fs',- [SetInlineFormatFontFamilyCommand.id]: 'ff',- [SetInlineFormatTextColorCommand.id]: 'cl',- };-switch (preCommandId) {case SetInlineFormatBoldCommand.id: // fallthroughcase SetInlineFormatItalicCommand.id: // fallthroughcase SetInlineFormatUnderlineCommand.id: // fallthrough- case SetInlineFormatStrikethroughCommand.id: {+ case SetInlineFormatStrikethroughCommand.id: // fallthrough+ case SetInlineFormatSubscriptCommand.id: // fallthrough+ case SetInlineFormatSuperscriptCommand.id: {formatValue = getReverseFormatValueInSelection(- docsModel.body!.textRuns!,- COMMAND_ID_TO_FORMAT_KEY_MAP[preCommandId],+ docsModel.getBody()!.textRuns!,+ preCommandId,selections);@@ -207,6 +224,8 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {SetInlineFormatBoldCommand.id,SetInlineFormatFontSizeCommand.id,SetInlineFormatFontFamilyCommand.id,+ SetInlineFormatSubscriptCommand.id,+ SetInlineFormatSuperscriptCommand.id,];/**@@ -257,11 +276,12 @@ function isTextDecoration(value: unknown | ITextDecoration): value is ITextDecor*/function getReverseFormatValueInSelection(textRuns: ITextRun[],- key: keyof IStyleBase,+ preCommandId: string,selections: TextRange[]-): BooleanNumber | ITextDecoration {+): BooleanNumber | ITextDecoration | BaselineOffset {let ti = 0;let si = 0;+ const key: keyof IStyleBase = COMMAND_ID_TO_FORMAT_KEY_MAP[preCommandId];while (ti !== textRuns.length && si !== selections.length) {const { startOffset, endOffset } = selections[si];@@ -275,30 +295,50 @@ function getReverseFormatValueInSelection(ti++;} else {if (ts?.[key] == null) {- return /bl|it/.test(key)- ? BooleanNumber.TRUE- : {- s: BooleanNumber.TRUE,- };+ if (/bl|it/.test(key)) {+ return BooleanNumber.TRUE;+ }++ if (/ul|st/.test(key)) {+ return {+ s: BooleanNumber.TRUE,+ };+ }++ if (/va/.test(key)) {+ return preCommandId === SetInlineFormatSubscriptCommand.id+ ? BaselineOffset.SUBSCRIPT+ : BaselineOffset.SUPERSCRIPT;+ }}- if (ts[key] === BooleanNumber.FALSE) {- return BooleanNumber.TRUE;- }-- if (isTextDecoration(ts[key]) && (ts[key] as ITextDecoration).s === BooleanNumber.FALSE) {+ if (isTextDecoration(ts?.[key]) && (ts[key] as ITextDecoration).s === BooleanNumber.FALSE) {return {s: BooleanNumber.TRUE,};}+ if (preCommandId === SetInlineFormatSubscriptCommand.id && ts?.[key] !== BaselineOffset.SUBSCRIPT) {+ return BaselineOffset.SUBSCRIPT;+ }++ if (preCommandId === SetInlineFormatSuperscriptCommand.id && ts?.[key] !== BaselineOffset.SUPERSCRIPT) {+ return BaselineOffset.SUPERSCRIPT;+ }++ if (ts?.[key] === BooleanNumber.FALSE) {+ return BooleanNumber.TRUE;+ }+ti++;}}return /bl|it/.test(key)? BooleanNumber.FALSE- : {- s: BooleanNumber.FALSE,- };+ : /ul|st/.test(key)+ ? {+ s: BooleanNumber.FALSE,+ }+ : BaselineOffset.NORMAL;}diff --git a/packages/docs/src/controllers/inline-format.controller.ts b/packages/docs/src/controllers/inline-format.controller.tsindex d5115eeff3..08749b560a 100644--- a/packages/docs/src/controllers/inline-format.controller.ts+++ b/packages/docs/src/controllers/inline-format.controller.ts@@ -25,6 +25,8 @@ import {SetInlineFormatFontSizeCommand,SetInlineFormatItalicCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatTextColorCommand,SetInlineFormatUnderlineCommand,} from '../commands/commands/inline-format.command';@@ -52,6 +54,8 @@ export class InlineFormatController extends Disposable {SetInlineFormatItalicCommand.id,SetInlineFormatUnderlineCommand.id,SetInlineFormatStrikethroughCommand.id,+ SetInlineFormatSubscriptCommand.id,+ SetInlineFormatSuperscriptCommand.id,SetInlineFormatFontSizeCommand.id,SetInlineFormatFontFamilyCommand.id,SetInlineFormatTextColorCommand.id,diff --git a/packages/docs/src/doc-plugin.ts b/packages/docs/src/doc-plugin.tsindex e90ced7c32..f5e1c08615 100644--- a/packages/docs/src/doc-plugin.ts+++ b/packages/docs/src/doc-plugin.ts@@ -44,6 +44,8 @@ import {SetInlineFormatFontSizeCommand,SetInlineFormatItalicCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatTextColorCommand,SetInlineFormatUnderlineCommand,} from './commands/commands/inline-format.command';@@ -129,6 +131,8 @@ export class UniverDocsPlugin extends Plugin {SetInlineFormatItalicCommand,SetInlineFormatUnderlineCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatFontSizeCommand,SetInlineFormatFontFamilyCommand,SetInlineFormatTextColorCommand,diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.tsindex 15247d9273..28872c1cea 100644--- a/packages/docs/src/index.ts+++ b/packages/docs/src/index.ts@@ -36,6 +36,8 @@ export {SetInlineFormatFontSizeCommand,SetInlineFormatItalicCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatTextColorCommand,SetInlineFormatUnderlineCommand,} from './commands/commands/inline-format.command';diff --git a/packages/sheets-ui/src/commands/commands/inline-format.command.ts b/packages/sheets-ui/src/commands/commands/inline-format.command.tsindex 047be2977a..62360e53ed 100644--- a/packages/sheets-ui/src/commands/commands/inline-format.command.ts+++ b/packages/sheets-ui/src/commands/commands/inline-format.command.ts@@ -22,6 +22,8 @@ import {SetInlineFormatFontSizeCommand,SetInlineFormatItalicCommand,SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,SetInlineFormatTextColorCommand,SetInlineFormatUnderlineCommand,} from '@univerjs/docs';@@ -105,6 +107,38 @@ export const SetRangeStrickThroughCommand: ICommand = {},};+export const SetRangeSubscriptCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.set-range-subscript',+ handler: async (accessor) => {+ const commandService = accessor.get(ICommandService);+ const contextService = accessor.get(IContextService);+ const isCellEditorFocus = contextService.getContextValue(FOCUSING_EDITOR);++ if (isCellEditorFocus) {+ return commandService.executeCommand(SetInlineFormatSubscriptCommand.id);+ }++ return false;+ },+};++export const SetRangeSuperscriptCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.set-range-superscript',+ handler: async (accessor) => {+ const commandService = accessor.get(ICommandService);+ const contextService = accessor.get(IContextService);+ const isCellEditorFocus = contextService.getContextValue(FOCUSING_EDITOR);++ if (isCellEditorFocus) {+ return commandService.executeCommand(SetInlineFormatSuperscriptCommand.id);+ }++ return false;+ },+};+export const SetRangeFontSizeCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.set-range-fontsize',diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.tsindex 3dfc1875fa..e19c072720 100644--- a/packages/sheets-ui/src/controllers/menu/menu.ts+++ b/packages/sheets-ui/src/controllers/menu/menu.ts@@ -66,6 +66,8 @@ import {SetRangeFontSizeCommand,SetRangeItalicCommand,SetRangeStrickThroughCommand,+ SetRangeSubscriptCommand,+ SetRangeSuperscriptCommand,SetRangeTextColorCommand,SetRangeUnderlineCommand,} from '../../commands/commands/inline-format.command';@@ -309,6 +311,72 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonIt};}+export function SubScriptMenuItemFactory(accessor: IAccessor): IMenuButtonItem {+ const commandService = accessor.get(ICommandService);+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const sheetPermissionService = accessor.get(SheetPermissionService);+ const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const sheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ return {+ id: SetRangeSubscriptCommand.id,+ group: MenuGroup.TOOLBAR_FORMAT,+ type: MenuItemType.BUTTON,+ icon: 'FontSizeReduceSingleSingle',+ tooltip: 'toolbar.subscript',+ positions: [MenuPosition.TOOLBAR_START],+ disabled$: new Observable<boolean>((subscriber) => {+ const permission$ = sheetPermissionService.getEditable$(unitId, sheetId)?.subscribe((e) => {+ subscriber.next(!e.value);+ });++ return () => {+ permission$?.unsubscribe();+ };+ }),+ activated$: new Observable<boolean>((subscriber) => {+ const disposable = commandService.onCommandExecuted((c) => {+ // TODO+ });++ subscriber.next(false);+ return disposable.dispose;+ }),+ };+}++export function SuperScriptMenuItemFactory(accessor: IAccessor): IMenuButtonItem {+ const commandService = accessor.get(ICommandService);+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const sheetPermissionService = accessor.get(SheetPermissionService);+ const unitId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();+ const sheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();+ return {+ id: SetRangeSuperscriptCommand.id,+ group: MenuGroup.TOOLBAR_FORMAT,+ type: MenuItemType.BUTTON,+ icon: 'FontSizeIncreaseSingle',+ tooltip: 'toolbar.superscript',+ positions: [MenuPosition.TOOLBAR_START],+ disabled$: new Observable<boolean>((subscriber) => {+ const permission$ = sheetPermissionService.getEditable$(unitId, sheetId)?.subscribe((e) => {+ subscriber.next(!e.value);+ });++ return () => {+ permission$?.unsubscribe();+ };+ }),+ activated$: new Observable<boolean>((subscriber) => {+ const disposable = commandService.onCommandExecuted((c) => {+ // TODO+ });++ subscriber.next(false);+ return disposable.dispose;+ }),+ };+}+export const FONT_SIZE_CHILDREN = [{label: '9',diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.tsindex 9fb133e294..443d75d854 100644--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts@@ -41,6 +41,8 @@ import {SetRangeFontSizeCommand,SetRangeItalicCommand,SetRangeStrickThroughCommand,+ SetRangeSubscriptCommand,+ SetRangeSuperscriptCommand,SetRangeTextColorCommand,SetRangeUnderlineCommand,} from '../commands/commands/inline-format.command';@@ -143,6 +145,8 @@ import {ShowColMenuItemFactory,ShowRowMenuItemFactory,StrikeThroughMenuItemFactory,+ SubScriptMenuItemFactory,+ SuperScriptMenuItemFactory,TextColorSelectorMenuItemFactory,TextRotateMenuItemFactory,UnderlineMenuItemFactory,@@ -254,6 +258,8 @@ export class SheetUIController extends Disposable {SetRangeItalicCommand,SetRangeUnderlineCommand,SetRangeStrickThroughCommand,+ SetRangeSubscriptCommand,+ SetRangeSuperscriptCommand,SetRangeFontSizeCommand,SetRangeFontFamilyCommand,SetRangeTextColorCommand,@@ -331,6 +337,8 @@ export class SheetUIController extends Disposable {ItalicMenuItemFactory,UnderlineMenuItemFactory,StrikeThroughMenuItemFactory,+ SubScriptMenuItemFactory,+ SuperScriptMenuItemFactory,FontFamilySelectorMenuItemFactory,FontSizeSelectorMenuItemFactory,ResetTextColorMenuItemFactory,diff --git a/packages/sheets-ui/src/locale/en-US.ts b/packages/sheets-ui/src/locale/en-US.tsindex 9d35648800..df1d9ac574 100644--- a/packages/sheets-ui/src/locale/en-US.ts+++ b/packages/sheets-ui/src/locale/en-US.ts@@ -32,6 +32,8 @@ export default {bold: 'Bold',italic: 'Italic',strikethrough: 'Strikethrough',+ subscript: 'Subscript',+ superscript: 'Superscript',underline: 'Underline',textColor: {main: 'Text color',diff --git a/packages/sheets-ui/src/locale/zh-CN.ts b/packages/sheets-ui/src/locale/zh-CN.tsindex 9149e2fbab..28948d33b3 100644--- a/packages/sheets-ui/src/locale/zh-CN.ts+++ b/packages/sheets-ui/src/locale/zh-CN.ts@@ -33,6 +33,8 @@ export default {bold: '粗体',italic: '斜体',strikethrough: '删除线',+ subscript: '下标',+ superscript: '上标',underline: '下划线',textColor: {main: '文本颜色',diff --git a/packages/ui/src/common/component-manager.ts b/packages/ui/src/common/component-manager.tsindex daaffb2f7f..11aadc6d2f 100644--- a/packages/ui/src/common/component-manager.ts+++ b/packages/ui/src/common/component-manager.ts@@ -41,6 +41,8 @@ import {DeleteRow,DownBorder,FontColor,+ FontSizeIncreaseSingle,+ FontSizeReduceSingleSingle,FreezeColumnSingle,FreezeRowSingle,FreezeToSelectedSingle,@@ -175,6 +177,8 @@ export class ComponentManager {FreezeRowSingle,FreezeToSelectedSingle,CodeSingle,+ FontSizeIncreaseSingle,+ FontSizeReduceSingleSingle,};for (const k in iconList) {diff --git a/packages/ui/src/utils/cell.ts b/packages/ui/src/utils/cell.tsindex 7ecb6660e5..bca483809e 100644--- a/packages/ui/src/utils/cell.ts+++ b/packages/ui/src/utils/cell.ts@@ -24,7 +24,7 @@ import type {ITextDecoration,ITextRun,} from '@univerjs/core';-import { getBorderStyleType, Tools } from '@univerjs/core';+import { BaselineOffset, getBorderStyleType, Tools } from '@univerjs/core';import { ptToPx, pxToPt } from '@univerjs/engine-render';import { textTrim } from './util';@@ -296,9 +296,9 @@ export function handleStringToStyle($dom?: HTMLElement, cssStyle: string = '') {if (key === 'vertical-align') {if (value === 'sub') {- styleList.va = 1;+ styleList.va = BaselineOffset.SUBSCRIPT;} else if (value === 'super') {- styleList.va = 2;+ styleList.va = BaselineOffset.SUPERSCRIPT;} else if (value === 'top') {styleList.vt = 1;} else if (value === 'middle') {@@ -306,7 +306,7 @@ export function handleStringToStyle($dom?: HTMLElement, cssStyle: string = '') {} else if (value === 'bottom') {styleList.vt = 3;} else {- styleList.va = 0;+ styleList.va = BaselineOffset.NORMAL;}}