Skip to content

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.

fix(docs): link break in link (#3254)

Ran Luoff178948759e
diff --git a/examples/src/data/docs/default-document-data-cn.ts b/examples/src/data/docs/default-document-data-cn.ts
index 80a9d77454..af2f6c22cb 100644
--- a/examples/src/data/docs/default-document-data-cn.ts
+++ b/examples/src/data/docs/default-document-data-cn.ts
@@ -619,6 +619,9 @@ export const DEFAULT_DOCUMENT_DATA_CN: IDocumentData = {
rgb: '#ccc',
},
},
+ autoHyphenation: BooleanNumber.TRUE,
+ doNotHyphenateCaps: BooleanNumber.FALSE,
+ consecutiveHyphenLimit: 2,
defaultHeaderId: '',
defaultFooterId: '',
evenPageHeaderId: '',
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/line-adjustment.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/line-adjustment.ts
index 97f5376288..a7c05c87b0 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/line-adjustment.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/line-adjustment.ts
@@ -24,7 +24,7 @@ import { BreakPointType } from '../../line-breaker/break';
import type { DocumentViewModel } from '../../../view-model/document-view-model';
import type { DataStreamTreeNode } from '../../../view-model/data-stream-tree-node';
import type { ISectionBreakConfig } from '../../../../../basics';
-import { isLetter } from '../../line-breaker/enhancers/hyphen-enhancer';
+import { isLetter } from '../../line-breaker/enhancers/utils';
// How much a character should hang into the end margin.
// For more discussion, see:
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
index 1e582606ad..cf94d1a22c 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
@@ -35,6 +35,7 @@ import { Lang } from '../../hyphenation/lang';
import { BreakPointType } from '../../line-breaker/break';
import { getBoundingBox } from '../../model/line';
import { customBlockLineBreakExtension } from '../../line-breaker/extensions/custom-block-linebreak-extension';
+import { LineBreakerLinkEnhancer } from '../../line-breaker/enhancers/link-enhancer';
import { ArabicHandler, emojiHandler, otherHandler, TibetanHandler } from './language-ruler';
// Now we apply consecutive punctuation adjustment, specified in Chinese Layout
@@ -145,6 +146,8 @@ export function shaping(
tabLineBreakExtension(breaker);
customBlockLineBreakExtension(breaker);
+ breaker = new LineBreakerLinkEnhancer(breaker) as unknown as LineBreaker;
+
const lang = languageDetector.detect(content);
const needHyphen = hyphenConfig(paragraphStyle, sectionBreakConfig);
diff --git a/packages/engine-render/src/components/docs/layout/line-breaker/break.ts b/packages/engine-render/src/components/docs/layout/line-breaker/break.ts
index f4523fc971..04ab6c2dbe 100644
--- a/packages/engine-render/src/components/docs/layout/line-breaker/break.ts
+++ b/packages/engine-render/src/components/docs/layout/line-breaker/break.ts
@@ -21,6 +21,8 @@ export enum BreakPointType {
Mandatory = 'Mandatory',
// An opportunity for hyphenating.
Hyphen = 'Hyphen',
+ // An opportunity for breaking in a link. *Hyphen is not allowed in a link*.
+ Link = 'Link',
}
export class Break {
diff --git a/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/hyphen-enhancer.ts b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/hyphen-enhancer.ts
index e7f53f5721..e3e03c5647 100644
--- a/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/hyphen-enhancer.ts
+++ b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/hyphen-enhancer.ts
@@ -19,34 +19,7 @@ import { Break, BreakPointType } from '../break';
import type { IBreakPoints, LineBreaker } from '../line-breaker';
import type { Hyphen } from '../../hyphenation/hyphen';
import type { Lang } from '../../hyphenation/lang';
-
-export function isLetter(char: string) {
- return char.length > 0 && !/\s|(?![\'])[\!-\@\[-\`\{-\~\u2013-\u203C]/.test(char);
-}
-
-function getWord(str: string): string {
- let word = '';
-
- for (let i = 0; i < str.length; i++) {
- if (isLetter(str[i])) {
- word += str[i];
- } else {
- break;
- }
- }
-
- return word;
-}
-
-function getHyphenPosition(lastPos: number, hyphenSlice: string[], index: number): number {
- let hyphenPos = lastPos;
-
- for (let i = 0; i <= index; i++) {
- hyphenPos += hyphenSlice[i].length;
- }
-
- return hyphenPos;
-}
+import { getSlicePosition, getWord } from './utils';
function isUpperCase(word: string) {
return word.length > 0 && word === word.toUpperCase();
@@ -65,7 +38,7 @@ export class LineBreakerHyphenEnhancer implements IBreakPoints {
private _hyphenSlice: string[] = [];
- private _content = '';
+ public content = '';
constructor(
private _lineBreaker: LineBreaker,
@@ -73,7 +46,7 @@ export class LineBreakerHyphenEnhancer implements IBreakPoints {
private _lang: Lang,
private _doNotHyphenateCaps = false
) {
- this._content = _lineBreaker.content;
+ this.content = _lineBreaker.content;
}
nextBreakPoint(): Nullable<Break> {
@@ -86,9 +59,9 @@ export class LineBreakerHyphenEnhancer implements IBreakPoints {
return null;
}
// Check if next break is in word.
- const word = getWord(this._content.slice(this._curBreak.position, this._nextBreak.position));
+ const word = getWord(this.content.slice(this._curBreak.position, this._nextBreak.position));
- if (word.length && !(isUpperCase(word) && this._doNotHyphenateCaps)) {
+ if (word.length && !(isUpperCase(word) && this._doNotHyphenateCaps) && this._nextBreak.type !== BreakPointType.Link) {
this._isInWord = true;
this._word = word;
// hyphenation.
@@ -101,7 +74,7 @@ export class LineBreakerHyphenEnhancer implements IBreakPoints {
} else {
// No need to add `-` to last hyphen slice, so use length - 1.
if (this._hyphenIndex < this._hyphenSlice.length - 1) {
- const position = getHyphenPosition(this._curBreak!.position, this._hyphenSlice, this._hyphenIndex);
+ const position = getSlicePosition(this._curBreak!.position, this._hyphenSlice, this._hyphenIndex);
this._hyphenIndex++;
diff --git a/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/link-enhancer.ts b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/link-enhancer.ts
new file mode 100644
index 0000000000..57ae6caa79
--- /dev/null
+++ b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/link-enhancer.ts
@@ -0,0 +1,167 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Nullable } from '@univerjs/core';
+import type { IBreakPoints, LineBreaker } from '../line-breaker';
+import { Break, BreakPointType } from '../break';
+import { getSlicePosition } from './utils';
+
+enum LinkCharType {
+ Alphabetic = 'Alphabetic',
+ Digit = 'Digit',
+ Open = 'Open',
+ Other = 'Other',
+}
+
+function ofLinkCharType(char: string): LinkCharType {
+ if (/[a-z]/i.test(char)) {
+ return LinkCharType.Alphabetic;
+ } else if (/[0-9]/.test(char)) {
+ return LinkCharType.Digit;
+ } else if (char === '(' || char === '[') {
+ return LinkCharType.Open;
+ } else {
+ return LinkCharType.Other;
+ }
+}
+
+function linebreakLink(link: string): string[] {
+ const pieces: string[] = [];
+ let offset = 0;
+ let prevCharType = LinkCharType.Other;
+
+ for (let i = 0; i < link.length; i++) {
+ const char = link[i];
+ const charType = ofLinkCharType(char);
+
+ // Emit opportunities when going from
+ // - other -> other
+ // - alphabetic -> numeric
+ // - numeric -> alphabetic
+ // Never before after opening delimiters.
+ if (
+ i > 0
+ && prevCharType !== LinkCharType.Open
+ && (charType === LinkCharType.Other ? charType === LinkCharType.Other : charType !== prevCharType)
+ ) {
+ const piece = link.slice(offset, i);
+ if (piece.length < 16) {
+ pieces.push(piece);
+ } else {
+ // If it gets very long (e.g. a hash in the URL), just allow a
+ // break at every char.
+ for (let j = 0; j < piece.length; j++) {
+ pieces.push(piece[j]);
+ }
+ }
+
+ offset = i;
+ prevCharType = charType;
+ }
+ }
+
+ return pieces;
+}
+
+const LINK_CHAR_REG_EXP = /[a-z\d!#$%&*+,-./:;=?@_~\\]/i;
+const LINK_CHAR_NO_TRAILING_REG_EXP = /[!,.;:?']$/g;
+
+function extractLink(content: string, offset: number): string {
+ let link = '';
+
+ for (let i = offset; i < content.length; i++) {
+ const char = content[i];
+ if (LINK_CHAR_REG_EXP.test(char)) {
+ link += char;
+ } else {
+ break;
+ }
+ }
+
+ link.replace(LINK_CHAR_NO_TRAILING_REG_EXP, '');
+
+ return link;
+}
+
+// Special case for links. UAX #14 doesn't handle them well.
+
+export class LineBreakerLinkEnhancer implements IBreakPoints {
+ private _curBreak: Nullable<Break> = null;
+
+ private _nextBreak: Nullable<Break> = new Break(0);
+
+ private _isInLink = false;
+
+ private _link = '';
+
+ private _index = -1;
+
+ private _linkSlice: string[] = [];
+
+ public content = '';
+
+ constructor(
+ private _lineBreaker: LineBreaker
+ ) {
+ this.content = _lineBreaker.content;
+ }
+
+ nextBreakPoint(): Nullable<Break> {
+ if (!this._isInLink) {
+ this._curBreak = this._nextBreak;
+ this._nextBreak = this._lineBreaker.nextBreakPoint();
+
+ // If has no next break, return null.
+ if (this._nextBreak == null || this._curBreak == null) {
+ return null;
+ }
+ // Check if next break is in word.
+ const word = this.content.slice(this._curBreak.position, this._nextBreak.position);
+
+ if (word.length && (word.endsWith('://') || word.startsWith('www.'))) {
+ this._isInLink = true;
+ this._link = extractLink(this.content, this._curBreak.position);
+ // get link slice.
+ this._linkSlice = linebreakLink(this._link);
+ this._index = 0;
+
+ while (this._nextBreak && this._nextBreak.position < this._curBreak.position + this._link.length) {
+ this._nextBreak = this._lineBreaker.nextBreakPoint();
+ }
+
+ return this.nextBreakPoint();
+ } else {
+ return this._nextBreak;
+ }
+ } else {
+ // No need to add `-` to last hyphen slice, so use length - 1.
+ if (this._index < this._linkSlice.length - 1) {
+ const position = getSlicePosition(this._curBreak!.position, this._linkSlice, this._index);
+
+ this._index++;
+
+ return new Break(position, BreakPointType.Link);
+ } else {
+ this._isInLink = false;
+ this._link = '';
+ this._linkSlice = [];
+ this._index = -1;
+
+ return this._nextBreak;
+ }
+ }
+ }
+}
diff --git a/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/utils.ts b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/utils.ts
new file mode 100644
index 0000000000..f286c9a248
--- /dev/null
+++ b/packages/engine-render/src/components/docs/layout/line-breaker/enhancers/utils.ts
@@ -0,0 +1,43 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export function isLetter(char: string) {
+ return char.length > 0 && !/\s|(?![\'])[\!-\@\[-\`\{-\~\u2013-\u203C]/.test(char);
+}
+
+export function getWord(str: string): string {
+ let word = '';
+
+ for (let i = 0; i < str.length; i++) {
+ if (isLetter(str[i])) {
+ word += str[i];
+ } else {
+ break;
+ }
+ }
+
+ return word;
+}
+
+export function getSlicePosition(lastPos: number, hyphenSlice: string[], index: number): number {
+ let position = lastPos;
+
+ for (let i = 0; i <= index; i++) {
+ position += hyphenSlice[i].length;
+ }
+
+ return position;
+}