@fileverse-dev/fortune-core 1.3.4-viewMode-5 → 1.3.5-hyper

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/api/sheet.js CHANGED
@@ -1,3 +1,14 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var __assign = this && this.__assign || function () {
3
+ __assign = Object.assign || function (t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
1
12
  import _ from "lodash";
2
13
  import { v4 as uuidv4 } from "uuid";
3
14
  import { dataToCelldata, getSheet } from "./common";
@@ -135,6 +146,7 @@ function generateCopySheetName(ctx, sheetId) {
135
146
  return sheetCopyName;
136
147
  }
137
148
  export function copySheet(ctx, sheetId) {
149
+ var _a, _b, _c;
138
150
  var index = getSheetIndex(ctx, sheetId);
139
151
  var order = ctx.luckysheetfile[index].order + 1;
140
152
  var sheetName = generateCopySheetName(ctx, sheetId);
@@ -144,8 +156,32 @@ export function copySheet(ctx, sheetId) {
144
156
  sheetData.celldata = dataToCelldata(sheetData.data);
145
157
  delete sheetData.data;
146
158
  api.addSheet(ctx, undefined, uuidv4(), ctx.luckysheetfile[index].isPivotTable, sheetName, sheetData);
159
+ var newSheetIndex = ctx.luckysheetfile.length - 1;
160
+ var newSheet = ctx.luckysheetfile[newSheetIndex];
161
+ var newSheetId = newSheet.id;
162
+ if ((_a = newSheet.calcChain) === null || _a === void 0 ? void 0 : _a.length) {
163
+ newSheet.calcChain = newSheet.calcChain.map(function (entry) {
164
+ return __assign(__assign({}, entry), {
165
+ id: newSheetId
166
+ });
167
+ });
168
+ }
169
+ if ((_b = newSheet.dynamicArray) === null || _b === void 0 ? void 0 : _b.length) {
170
+ newSheet.dynamicArray = newSheet.dynamicArray.map(function (entry) {
171
+ return entry && _typeof(entry) === "object" && "id" in entry ? __assign(__assign({}, entry), {
172
+ id: newSheetId
173
+ }) : entry;
174
+ });
175
+ }
176
+ if ((_c = newSheet.dynamicArray_compute) === null || _c === void 0 ? void 0 : _c.length) {
177
+ newSheet.dynamicArray_compute = newSheet.dynamicArray_compute.map(function (entry) {
178
+ return entry && _typeof(entry) === "object" && "id" in entry ? __assign(__assign({}, entry), {
179
+ id: newSheetId
180
+ }) : entry;
181
+ });
182
+ }
147
183
  var sheetOrderList = {};
148
- sheetOrderList[ctx.luckysheetfile[ctx.luckysheetfile.length - 1].id] = order;
184
+ sheetOrderList[newSheetId] = order;
149
185
  api.setSheetOrder(ctx, sheetOrderList);
150
186
  }
151
187
  export function calculateSheetFromula(ctx, id) {
@@ -396,7 +396,7 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
396
396
  textFormat(ctx, "right");
397
397
  }
398
398
  if ((e.metaKey || e.ctrlKey) && e.code === "KeyK") {
399
- handleLink(ctx);
399
+ handleLink(ctx, cellInput);
400
400
  }
401
401
  if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.code === "Semicolon") {
402
402
  fillDate(ctx);
@@ -145,7 +145,7 @@ export function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, containe
145
145
  _a = margeset.row, row_pre = _a[0], row = _a[1], row_index = _a[2], row_index_ed = _a[3];
146
146
  _b = margeset.column, col_pre = _b[0], col = _b[1], col_index = _b[2], col_index_ed = _b[3];
147
147
  }
148
- showLinkCard(ctx, row_index, col_index, false, true);
148
+ showLinkCard(ctx, row_index, col_index, undefined, false, true);
149
149
  if (((_e = (_d = ctx.hooks).beforeCellMouseDown) === null || _e === void 0 ? void 0 : _e.call(_d, (_f = flowdata[row_index]) === null || _f === void 0 ? void 0 : _f[col_index], {
150
150
  row: row_index,
151
151
  column: col_index,
@@ -78,11 +78,9 @@ export function adjustFormulaForPaste(formula, srcCol, srcRow, destCol, destRow)
78
78
  var rowOffset = destRow - srcRow;
79
79
  var hadInvalid = false;
80
80
  var cellRefRegex = /\b(\$?)([A-Z]+)(\$?)(\d+)\b/g;
81
- var stringOrCellRef = /"(?:\\.|[^"])*"|(?<!\$)([A-Z]+\d+\b)/g;
81
+ var stringOrCellRef = /"(?:\\.|[^"])*"|(\$?[A-Z]+\$?\d+)(?!\s*!)\b/g;
82
82
  var result = formula.replace(stringOrCellRef, function (m, cellRef) {
83
83
  if (!cellRef) return m;
84
- if (cellRef.startsWith("$")) return m;
85
- console.log(m, "cellRef", cellRef);
86
84
  return cellRef.replace(cellRefRegex, function (__, absCol, colLetters, absRow, rowNum) {
87
85
  var colIndex = columnLabelIndex(colLetters);
88
86
  var rowIndex = parseInt(rowNum, 10);
@@ -1011,9 +1009,10 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
1011
1009
  if (!_.isNil(value) && !_.isNil(value.f)) {
1012
1010
  var adjustedFormula = value.f;
1013
1011
  var isError = false;
1012
+ var srcRow = c_r1 + (h - mth);
1013
+ var srcCol = c_c1 + (c - mtc);
1014
1014
  try {
1015
- adjustedFormula = adjustFormulaForPaste(value.f, c_c1, c_r1, c, h);
1016
- console.log("adjustedFormula", adjustedFormula);
1015
+ adjustedFormula = adjustFormulaForPaste(value.f, srcCol, srcRow, c, h);
1017
1016
  } catch (error) {
1018
1017
  isError = true;
1019
1018
  value.error = {
@@ -1122,10 +1122,16 @@ export function getInlineStringHTML(r, c, data) {
1122
1122
  var strObj = strings[i];
1123
1123
  if (strObj.v) {
1124
1124
  var style = getFontStyleByCell(strObj);
1125
+ var link = strObj.link;
1126
+ if ((link === null || link === void 0 ? void 0 : link.linkType) && (link === null || link === void 0 ? void 0 : link.linkAddress)) {
1127
+ style.color = style.color || "rgb(0, 0, 255)";
1128
+ style.borderBottom = style.borderBottom || "1px solid rgb(0, 0, 255)";
1129
+ }
1125
1130
  var styleStr = _.map(style, function (v, key) {
1126
1131
  return "".concat(_.kebabCase(key), ":").concat(_.isNumber(v) ? "".concat(v, "px") : v, ";");
1127
1132
  }).join("");
1128
- value += "<span class=\"luckysheet-input-span\" index='".concat(i, "' style='").concat(styleStr, "'>").concat(strObj.v, "</span>");
1133
+ var dataAttrs = (link === null || link === void 0 ? void 0 : link.linkType) && (link === null || link === void 0 ? void 0 : link.linkAddress) ? " data-link-type='".concat(String(link.linkType).replace(/'/g, "&#39;"), "' data-link-address='").concat(String(link.linkAddress).replace(/'/g, "&#39;"), "'") : "";
1134
+ value += "<span class=\"luckysheet-input-span\" index='".concat(i, "' style='").concat(styleStr, "'").concat(dataAttrs, ">").concat(strObj.v, "</span>");
1129
1135
  }
1130
1136
  }
1131
1137
  return value;
@@ -1,3 +1,8 @@
1
1
  export declare function moveToEnd(obj: HTMLDivElement): void;
2
2
  export declare function selectTextContent(ele: HTMLElement): void;
3
3
  export declare function selectTextContentCross(sEle: HTMLElement, eEle: HTMLElement): void;
4
+ export declare function getSelectionCharacterOffsets(element: Node): {
5
+ start: number;
6
+ end: number;
7
+ } | null;
8
+ export declare function setSelectionByCharacterOffset(element: HTMLDivElement, start: number, end: number): void;
@@ -59,4 +59,59 @@ export function selectTextContentCross(sEle, eEle) {
59
59
  }
60
60
  }
61
61
  }
62
+ }
63
+ export function getSelectionCharacterOffsets(element) {
64
+ var sel = window.getSelection();
65
+ if (!sel || sel.rangeCount === 0) return null;
66
+ var range = sel.getRangeAt(0);
67
+ if (range.collapsed) return null;
68
+ if (!element.contains(range.startContainer) || !element.contains(range.endContainer)) {
69
+ return null;
70
+ }
71
+ var pre = document.createRange();
72
+ pre.selectNodeContents(element);
73
+ pre.setEnd(range.startContainer, range.startOffset);
74
+ var start = pre.toString().length;
75
+ return {
76
+ start: start,
77
+ end: start + range.toString().length
78
+ };
79
+ }
80
+ export function setSelectionByCharacterOffset(element, start, end) {
81
+ element.focus();
82
+ var sel = window.getSelection();
83
+ if (!sel) return;
84
+ var charIndex = 0;
85
+ var startNode = null;
86
+ var startOffset = 0;
87
+ var endNode = null;
88
+ var endOffset = 0;
89
+ function walk(node) {
90
+ if (node.nodeType === Node.TEXT_NODE) {
91
+ var len = (node.textContent || "").length;
92
+ if (startNode == null && charIndex + len > start) {
93
+ startNode = node;
94
+ startOffset = start - charIndex;
95
+ }
96
+ if (endNode == null && charIndex + len >= end) {
97
+ endNode = node;
98
+ endOffset = end - charIndex;
99
+ return true;
100
+ }
101
+ charIndex += len;
102
+ return false;
103
+ }
104
+ for (var i = 0; i < node.childNodes.length; i += 1) {
105
+ if (walk(node.childNodes[i])) return true;
106
+ }
107
+ return false;
108
+ }
109
+ walk(element);
110
+ if (startNode && endNode) {
111
+ var range = document.createRange();
112
+ range.setStart(startNode, startOffset);
113
+ range.setEnd(endNode, endOffset);
114
+ sel.removeAllRanges();
115
+ sel.addRange(range);
116
+ }
62
117
  }
@@ -8,9 +8,19 @@ export declare function getCellHyperlink(ctx: Context, r: number, c: number): {
8
8
  linkType: string;
9
9
  linkAddress: string;
10
10
  } | undefined;
11
- export declare function saveHyperlink(ctx: Context, r: number, c: number, linkText: string, linkType: string, linkAddress: string): void;
11
+ export declare function saveHyperlink(ctx: Context, r: number, c: number, linkText: string, linkType: string, linkAddress: string, options?: {
12
+ applyToSelection?: boolean;
13
+ cellInput?: HTMLDivElement | null;
14
+ }): void;
12
15
  export declare function removeHyperlink(ctx: Context, r: number, c: number): void;
13
- export declare function showLinkCard(ctx: Context, r: number, c: number, isEditing?: boolean, isMouseDown?: boolean): void;
16
+ export declare function showLinkCard(ctx: Context, r: number, c: number, options?: {
17
+ applyToSelection?: boolean;
18
+ originText?: string;
19
+ selectionOffsets?: {
20
+ start: number;
21
+ end: number;
22
+ };
23
+ }, isEditing?: boolean, isMouseDown?: boolean): void;
14
24
  export declare function goToLink(ctx: Context, r: number, c: number, linkType: string, linkAddress: string, scrollbarX: HTMLDivElement, scrollbarY: HTMLDivElement): void;
15
25
  export declare function isLinkValid(ctx: Context, linkType: string, linkAddress: string): {
16
26
  isValid: boolean;
@@ -1,8 +1,10 @@
1
1
  import _ from "lodash";
2
2
  import { getFlowdata } from "../context";
3
3
  import { getSheetIndex, isAllowEdit } from "../utils";
4
- import { mergeBorder } from "./cell";
4
+ import { cancelNormalSelected, mergeBorder } from "./cell";
5
+ import { setSelectionByCharacterOffset } from "./cursor";
5
6
  import { getcellrange, iscelldata } from "./formula";
7
+ import { applyLinkToSelection } from "./inline-string";
6
8
  import { colLocation, rowLocation } from "./location";
7
9
  import { normalizeSelection } from "./selection";
8
10
  import { changeSheet } from "./sheet";
@@ -38,9 +40,39 @@ export function getCellHyperlink(ctx, r, c) {
38
40
  }
39
41
  return undefined;
40
42
  }
41
- export function saveHyperlink(ctx, r, c, linkText, linkType, linkAddress) {
43
+ export function saveHyperlink(ctx, r, c, linkText, linkType, linkAddress, options) {
44
+ var _a;
45
+ var applyToSelection = (options === null || options === void 0 ? void 0 : options.applyToSelection) && (options === null || options === void 0 ? void 0 : options.cellInput);
42
46
  var sheetIndex = getSheetIndex(ctx, ctx.currentSheetId);
43
47
  var flowdata = getFlowdata(ctx);
48
+ if (applyToSelection) {
49
+ if (sheetIndex != null && flowdata != null && linkType && linkAddress) {
50
+ var cell = flowdata[r][c];
51
+ if (cell == null) cell = {};
52
+ _.set(ctx.luckysheetfile[sheetIndex], ["hyperlink", "".concat(r, "_").concat(c)], {
53
+ linkType: linkType,
54
+ linkAddress: linkAddress
55
+ });
56
+ cell.v = linkText || linkAddress;
57
+ cell.m = linkText || linkAddress;
58
+ cell.hl = {
59
+ r: r,
60
+ c: c,
61
+ id: ctx.currentSheetId
62
+ };
63
+ flowdata[r][c] = cell;
64
+ }
65
+ var offsets = (_a = ctx.linkCard) === null || _a === void 0 ? void 0 : _a.selectionOffsets;
66
+ if (offsets) {
67
+ setSelectionByCharacterOffset(options.cellInput, offsets.start, offsets.end);
68
+ }
69
+ applyLinkToSelection(options.cellInput, linkType, linkAddress);
70
+ ctx.linkCard = undefined;
71
+ return;
72
+ }
73
+ if (options === null || options === void 0 ? void 0 : options.cellInput) {
74
+ cancelNormalSelected(ctx);
75
+ }
44
76
  if (sheetIndex != null && flowdata != null && linkType && linkAddress) {
45
77
  var cell = flowdata[r][c];
46
78
  if (cell == null) cell = {};
@@ -79,8 +111,8 @@ export function removeHyperlink(ctx, r, c) {
79
111
  }
80
112
  ctx.linkCard = undefined;
81
113
  }
82
- export function showLinkCard(ctx, r, c, isEditing, isMouseDown) {
83
- var _a, _b, _c, _d, _e, _f, _g;
114
+ export function showLinkCard(ctx, r, c, options, isEditing, isMouseDown) {
115
+ var _a, _b, _c, _d, _e, _f, _g, _h;
84
116
  if (isEditing === void 0) {
85
117
  isEditing = false;
86
118
  }
@@ -98,19 +130,26 @@ export function showLinkCard(ctx, r, c, isEditing, isMouseDown) {
98
130
  if (isEditing || link != null && (!((_f = ctx.linkCard) === null || _f === void 0 ? void 0 : _f.isEditing) || isMouseDown) || ((_g = ctx.linkCard) === null || _g === void 0 ? void 0 : _g.sheetId) !== ctx.currentSheetId) {
99
131
  var col_pre = c - 1 === -1 ? 0 : ctx.visibledatacolumn[c - 1];
100
132
  var row = ctx.visibledatarow[r];
133
+ var originText = function () {
134
+ if ((options === null || options === void 0 ? void 0 : options.originText) !== undefined) return options.originText;
135
+ if ((cell === null || cell === void 0 ? void 0 : cell.v) == null) return "";
136
+ return "".concat(cell.v);
137
+ }();
101
138
  ctx.linkCard = {
102
139
  sheetId: ctx.currentSheetId,
103
140
  r: r,
104
141
  c: c,
105
142
  rc: "".concat(r, "_").concat(c),
106
- originText: (cell === null || cell === void 0 ? void 0 : cell.v) == null ? "" : "".concat(cell.v),
143
+ originText: originText,
107
144
  originType: (link === null || link === void 0 ? void 0 : link.linkType) || "webpage",
108
145
  originAddress: (link === null || link === void 0 ? void 0 : link.linkAddress) || "",
109
146
  position: {
110
147
  cellLeft: col_pre,
111
148
  cellBottom: row
112
149
  },
113
- isEditing: isEditing
150
+ isEditing: isEditing,
151
+ applyToSelection: (_h = options === null || options === void 0 ? void 0 : options.applyToSelection) !== null && _h !== void 0 ? _h : false,
152
+ selectionOffsets: options === null || options === void 0 ? void 0 : options.selectionOffsets
114
153
  };
115
154
  }
116
155
  }
@@ -31,6 +31,14 @@ export declare function isInlineStringCell(cell: any): boolean;
31
31
  export declare function isInlineStringCT(ct: any): boolean;
32
32
  export declare function getInlineStringNoStyle(r: number, c: number, data: CellMatrix): string;
33
33
  export declare function convertCssToStyleList(cssText: string, originCell: Cell): CellStyle;
34
- export declare function convertSpanToShareString($dom: NodeListOf<HTMLSpanElement>, originCell: Cell): CellStyle[];
34
+ export type InlineSegmentLink = {
35
+ linkType: string;
36
+ linkAddress: string;
37
+ };
38
+ export declare function convertSpanToShareString($dom: NodeListOf<HTMLSpanElement>, originCell: Cell): (CellStyle & {
39
+ v?: string | undefined;
40
+ link?: InlineSegmentLink | undefined;
41
+ })[];
35
42
  export declare function updateInlineStringFormatOutside(cell: Cell, key: string, value: any): void;
36
43
  export declare function updateInlineStringFormat(ctx: Context, attr: keyof Cell, value: any, cellInput: HTMLDivElement): void;
44
+ export declare function applyLinkToSelection(cellInput: HTMLDivElement, linkType: string, linkAddress: string): void;
@@ -101,13 +101,20 @@ export function convertCssToStyleList(cssText, originCell) {
101
101
  return styleList;
102
102
  }
103
103
  export function convertSpanToShareString($dom, originCell) {
104
+ var _a, _b;
104
105
  var styles = [];
105
- var preStyleList;
106
+ var preStyleList = null;
106
107
  var preStyleListString = null;
107
108
  for (var i = 0; i < $dom.length; i += 1) {
108
109
  var span = $dom[i];
109
110
  var styleList = convertCssToStyleList(span.style.cssText, originCell);
110
- var curStyleListString = JSON.stringify(styleList);
111
+ if (((_a = span.dataset) === null || _a === void 0 ? void 0 : _a.linkType) && ((_b = span.dataset) === null || _b === void 0 ? void 0 : _b.linkAddress)) {
112
+ styleList.link = {
113
+ linkType: span.dataset.linkType,
114
+ linkAddress: span.dataset.linkAddress
115
+ };
116
+ }
117
+ var curStyleListString = JSON.stringify(_.omit(styleList, "link"));
111
118
  var v = span.innerText;
112
119
  v = v.replace(/\n/g, "\r\n");
113
120
  if (i === $dom.length - 1) {
@@ -115,7 +122,7 @@ export function convertSpanToShareString($dom, originCell) {
115
122
  v = v.slice(0, v.length - 2);
116
123
  }
117
124
  }
118
- if (curStyleListString === preStyleListString) {
125
+ if (curStyleListString === preStyleListString && _.isEqual(preStyleList === null || preStyleList === void 0 ? void 0 : preStyleList.link, styleList.link)) {
119
126
  preStyleList.v += v;
120
127
  } else {
121
128
  styleList.v = v;
@@ -434,4 +441,128 @@ export function updateInlineStringFormat(ctx, attr, value, cellInput) {
434
441
  }
435
442
  }
436
443
  }
444
+ }
445
+ function escapeHtmlAttr(s) {
446
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
447
+ }
448
+ function getLinkDataAttrs(span) {
449
+ var _a, _b;
450
+ if (((_a = span.dataset) === null || _a === void 0 ? void 0 : _a.linkType) && ((_b = span.dataset) === null || _b === void 0 ? void 0 : _b.linkAddress)) {
451
+ return " data-link-type='".concat(escapeHtmlAttr(span.dataset.linkType), "' data-link-address='").concat(escapeHtmlAttr(span.dataset.linkAddress), "'");
452
+ }
453
+ return "";
454
+ }
455
+ function getLinkStyleCssText(baseCssText) {
456
+ var css = getCssText(baseCssText, "fc", "rgb(0, 0, 255)");
457
+ css = getCssText(css, "un", 1);
458
+ return css;
459
+ }
460
+ export function applyLinkToSelection(cellInput, linkType, linkAddress) {
461
+ var _a, _b, _c;
462
+ var w = window.getSelection();
463
+ if (!w || w.rangeCount === 0) return;
464
+ var range = w.getRangeAt(0);
465
+ if (range.collapsed) return;
466
+ var $textEditor = cellInput;
467
+ var endContainer = range.endContainer,
468
+ startContainer = range.startContainer,
469
+ endOffset = range.endOffset,
470
+ startOffset = range.startOffset;
471
+ if (startContainer === endContainer) {
472
+ var span = startContainer.parentNode;
473
+ var content = (span === null || span === void 0 ? void 0 : span.innerHTML) || "";
474
+ var fullContent = $textEditor.innerHTML;
475
+ var inherit = fullContent.substring(0, 5) !== "<span";
476
+ var s2 = startOffset;
477
+ var s3 = endOffset;
478
+ var left = content.substring(0, s2);
479
+ var mid = content.substring(s2, s3);
480
+ var right = content.substring(s3, content.length);
481
+ var cont = "";
482
+ var dataAttrs = getLinkDataAttrs(span);
483
+ if (left !== "") {
484
+ var cssText = span.style.cssText;
485
+ if (inherit) {
486
+ var box = span.closest("#luckysheet-input-box");
487
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
488
+ }
489
+ cont += "<span style='".concat(cssText, "'").concat(dataAttrs, ">").concat(left, "</span>");
490
+ }
491
+ if (mid !== "") {
492
+ var cssText = getLinkStyleCssText(span.style.cssText);
493
+ if (inherit) {
494
+ var box = span.closest("#luckysheet-input-box");
495
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
496
+ }
497
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(mid, "</span>");
498
+ }
499
+ if (right !== "") {
500
+ var cssText = span.style.cssText;
501
+ if (inherit) {
502
+ var box = span.closest("#luckysheet-input-box");
503
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
504
+ }
505
+ cont += "<span style='".concat(cssText, "'").concat(dataAttrs, ">").concat(right, "</span>");
506
+ }
507
+ if (((_a = startContainer.parentElement) === null || _a === void 0 ? void 0 : _a.tagName) === "SPAN") {
508
+ span.outerHTML = cont;
509
+ } else {
510
+ span.innerHTML = cont;
511
+ }
512
+ var newSpans = $textEditor.querySelectorAll("span");
513
+ var linkSpanIndex = left === "" ? 0 : 1;
514
+ if (newSpans[linkSpanIndex]) {
515
+ selectTextContent(newSpans[linkSpanIndex]);
516
+ }
517
+ } else if (((_b = startContainer.parentElement) === null || _b === void 0 ? void 0 : _b.tagName) === "SPAN" && ((_c = endContainer.parentElement) === null || _c === void 0 ? void 0 : _c.tagName) === "SPAN") {
518
+ var startSpan = startContainer.parentNode;
519
+ var endSpan = endContainer.parentNode;
520
+ var allSpans = $textEditor.querySelectorAll("span");
521
+ var startSpanIndex = _.indexOf(allSpans, startSpan);
522
+ var endSpanIndex = _.indexOf(allSpans, endSpan);
523
+ var startContent = (startSpan === null || startSpan === void 0 ? void 0 : startSpan.innerHTML) || "";
524
+ var endContent = (endSpan === null || endSpan === void 0 ? void 0 : endSpan.innerHTML) || "";
525
+ var s2 = startOffset;
526
+ var s3 = endOffset;
527
+ var sleft = startContent.substring(0, s2);
528
+ var sright = startContent.substring(s2, startContent.length);
529
+ var eleft = endContent.substring(0, s3);
530
+ var eright = endContent.substring(s3, endContent.length);
531
+ var spans = $textEditor.querySelectorAll("span");
532
+ var cont = "";
533
+ for (var i = 0; i < startSpanIndex; i += 1) {
534
+ var sp = spans[i];
535
+ cont += "<span style='".concat(sp.style.cssText, "'").concat(getLinkDataAttrs(sp), ">").concat(sp.innerHTML, "</span>");
536
+ }
537
+ if (sleft !== "") {
538
+ cont += "<span style='".concat(startSpan.style.cssText, "'").concat(getLinkDataAttrs(startSpan), ">").concat(sleft, "</span>");
539
+ }
540
+ if (sright !== "") {
541
+ var cssText = getLinkStyleCssText(startSpan.style.cssText);
542
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(sright, "</span>");
543
+ }
544
+ if (startSpanIndex < endSpanIndex) {
545
+ for (var i = startSpanIndex + 1; i < endSpanIndex; i += 1) {
546
+ var sp = spans[i];
547
+ var cssText = getLinkStyleCssText(sp.style.cssText);
548
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(sp.innerHTML, "</span>");
549
+ }
550
+ }
551
+ if (eleft !== "") {
552
+ var cssText = getLinkStyleCssText(endSpan.style.cssText);
553
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(eleft, "</span>");
554
+ }
555
+ if (eright !== "") {
556
+ cont += "<span style='".concat(endSpan.style.cssText, "'").concat(getLinkDataAttrs(endSpan), ">").concat(eright, "</span>");
557
+ }
558
+ for (var i = endSpanIndex + 1; i < spans.length; i += 1) {
559
+ var sp = spans[i];
560
+ cont += "<span style='".concat(sp.style.cssText, "'").concat(getLinkDataAttrs(sp), ">").concat(sp.innerHTML, "</span>");
561
+ }
562
+ $textEditor.innerHTML = cont;
563
+ spans = $textEditor.querySelectorAll("span");
564
+ var startSel = sleft === "" ? startSpanIndex : startSpanIndex + 1;
565
+ var endSel = eright === "" ? endSpanIndex : endSpanIndex + 1;
566
+ selectTextContentCross(spans[startSel], spans[endSel]);
567
+ }
437
568
  }
@@ -26,7 +26,7 @@ export declare function handleSort(ctx: Context, isAsc: boolean): void;
26
26
  export declare function handleFreeze(ctx: Context, type: string): void;
27
27
  export declare function handleTextSize(ctx: Context, cellInput: HTMLDivElement, size: number, canvas?: CanvasRenderingContext2D): void;
28
28
  export declare function handleSum(ctx: Context, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, cache?: GlobalCache): void;
29
- export declare function handleLink(ctx: Context): void;
29
+ export declare function handleLink(ctx: Context, cellInput: HTMLDivElement | null | undefined): void;
30
30
  export declare function toolbarItemClickHandler(name: string): ToolbarItemClickHandler;
31
31
  export declare function toolbarItemSelectedFunc(name: string): ToolbarItemSelectedFunc;
32
32
  export {};
@@ -5,6 +5,7 @@ import { getSheetIndex, isAllowEdit, getLineCount } from "../utils";
5
5
  import { getRangetxt, isAllSelectedCellsInStatus, normalizedAttr, setCellValue } from "./cell";
6
6
  import { colors } from "./color";
7
7
  import { genarate, is_date, update } from "./format";
8
+ import { getSelectionCharacterOffsets } from "./cursor";
8
9
  import { execfunction, execFunctionGroup, israngeseleciton, rangeSetValue, setCaretPosition, createFormulaRangeSelect } from "./formula";
9
10
  import { inlineStyleAffectAttribute, updateInlineStringFormat, updateInlineStringFormatOutside } from "./inline-string";
10
11
  import { colLocationByIndex, rowLocationByIndex } from "./location";
@@ -969,15 +970,36 @@ export function handleTextSize(ctx, cellInput, size, canvas) {
969
970
  export function handleSum(ctx, cellInput, fxInput, cache) {
970
971
  autoSelectionFormula(ctx, cellInput, fxInput, "SUM", cache);
971
972
  }
972
- export function handleLink(ctx) {
973
- var _a;
973
+ export function handleLink(ctx, cellInput) {
974
+ var _a, _b, _c;
974
975
  var allowEdit = isAllowEdit(ctx);
975
976
  if (!allowEdit) return;
976
977
  var selection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
977
978
  var flowdata = getFlowdata(ctx);
978
- if (flowdata != null && selection != null) {
979
- showLinkCard(ctx, selection.row[0], selection.column[0], true);
979
+ if (flowdata == null || selection == null) return;
980
+ var r = selection.row[0];
981
+ var c = selection.column[0];
982
+ var isEditMode = ((_b = ctx.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length) === 2 && ctx.luckysheetCellUpdate[0] === r && ctx.luckysheetCellUpdate[1] === c;
983
+ var applyToSelection = false;
984
+ var originText;
985
+ var selectionOffsets;
986
+ if (isEditMode && cellInput) {
987
+ var sel = window.getSelection();
988
+ if (sel && sel.rangeCount > 0 && !sel.getRangeAt(0).collapsed && cellInput.contains(sel.anchorNode)) {
989
+ var value = (_c = cellInput.innerText) !== null && _c !== void 0 ? _c : "";
990
+ if (value.substring(0, 1) !== "=") {
991
+ originText = sel.toString();
992
+ applyToSelection = true;
993
+ var off = getSelectionCharacterOffsets(cellInput);
994
+ if (off) selectionOffsets = off;
995
+ }
996
+ }
980
997
  }
998
+ showLinkCard(ctx, r, c, {
999
+ applyToSelection: applyToSelection || undefined,
1000
+ originText: originText,
1001
+ selectionOffsets: selectionOffsets
1002
+ }, true, false);
981
1003
  }
982
1004
  var handlerMap = {
983
1005
  "currency-format": handleCurrencyFormat,
package/es/types.d.ts CHANGED
@@ -269,6 +269,11 @@ export type LinkCardProps = {
269
269
  };
270
270
  isEditing: boolean;
271
271
  selectingCellRange?: boolean;
272
+ applyToSelection?: boolean;
273
+ selectionOffsets?: {
274
+ start: number;
275
+ end: number;
276
+ };
272
277
  };
273
278
  export type RangeDialogProps = {
274
279
  show: boolean;
package/lib/api/sheet.js CHANGED
@@ -22,6 +22,17 @@ var _common = require("./common");
22
22
  var _utils = require("../utils");
23
23
  var _2 = require("..");
24
24
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
25
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
26
+ var __assign = void 0 && (void 0).__assign || function () {
27
+ __assign = Object.assign || function (t) {
28
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
29
+ s = arguments[i];
30
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
31
+ }
32
+ return t;
33
+ };
34
+ return __assign.apply(this, arguments);
35
+ };
25
36
  function isCellReferenced(formulaString, cell) {
26
37
  function colToNumber(col) {
27
38
  var num = 0;
@@ -153,6 +164,7 @@ function generateCopySheetName(ctx, sheetId) {
153
164
  return sheetCopyName;
154
165
  }
155
166
  function copySheet(ctx, sheetId) {
167
+ var _a, _b, _c;
156
168
  var index = (0, _utils.getSheetIndex)(ctx, sheetId);
157
169
  var order = ctx.luckysheetfile[index].order + 1;
158
170
  var sheetName = generateCopySheetName(ctx, sheetId);
@@ -162,8 +174,32 @@ function copySheet(ctx, sheetId) {
162
174
  sheetData.celldata = (0, _common.dataToCelldata)(sheetData.data);
163
175
  delete sheetData.data;
164
176
  _2.api.addSheet(ctx, undefined, (0, _uuid.v4)(), ctx.luckysheetfile[index].isPivotTable, sheetName, sheetData);
177
+ var newSheetIndex = ctx.luckysheetfile.length - 1;
178
+ var newSheet = ctx.luckysheetfile[newSheetIndex];
179
+ var newSheetId = newSheet.id;
180
+ if ((_a = newSheet.calcChain) === null || _a === void 0 ? void 0 : _a.length) {
181
+ newSheet.calcChain = newSheet.calcChain.map(function (entry) {
182
+ return __assign(__assign({}, entry), {
183
+ id: newSheetId
184
+ });
185
+ });
186
+ }
187
+ if ((_b = newSheet.dynamicArray) === null || _b === void 0 ? void 0 : _b.length) {
188
+ newSheet.dynamicArray = newSheet.dynamicArray.map(function (entry) {
189
+ return entry && _typeof(entry) === "object" && "id" in entry ? __assign(__assign({}, entry), {
190
+ id: newSheetId
191
+ }) : entry;
192
+ });
193
+ }
194
+ if ((_c = newSheet.dynamicArray_compute) === null || _c === void 0 ? void 0 : _c.length) {
195
+ newSheet.dynamicArray_compute = newSheet.dynamicArray_compute.map(function (entry) {
196
+ return entry && _typeof(entry) === "object" && "id" in entry ? __assign(__assign({}, entry), {
197
+ id: newSheetId
198
+ }) : entry;
199
+ });
200
+ }
165
201
  var sheetOrderList = {};
166
- sheetOrderList[ctx.luckysheetfile[ctx.luckysheetfile.length - 1].id] = order;
202
+ sheetOrderList[newSheetId] = order;
167
203
  _2.api.setSheetOrder(ctx, sheetOrderList);
168
204
  }
169
205
  function calculateSheetFromula(ctx, id) {
@@ -406,7 +406,7 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
406
406
  (0, _selection.textFormat)(ctx, "right");
407
407
  }
408
408
  if ((e.metaKey || e.ctrlKey) && e.code === "KeyK") {
409
- (0, _toolbar.handleLink)(ctx);
409
+ (0, _toolbar.handleLink)(ctx, cellInput);
410
410
  }
411
411
  if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.code === "Semicolon") {
412
412
  (0, _selection.fillDate)(ctx);
@@ -166,7 +166,7 @@ function handleCellAreaMouseDown(ctx, globalCache, e, cellInput, container, fxIn
166
166
  _a = margeset.row, row_pre = _a[0], row = _a[1], row_index = _a[2], row_index_ed = _a[3];
167
167
  _b = margeset.column, col_pre = _b[0], col = _b[1], col_index = _b[2], col_index_ed = _b[3];
168
168
  }
169
- (0, _hyperlink.showLinkCard)(ctx, row_index, col_index, false, true);
169
+ (0, _hyperlink.showLinkCard)(ctx, row_index, col_index, undefined, false, true);
170
170
  if (((_e = (_d = ctx.hooks).beforeCellMouseDown) === null || _e === void 0 ? void 0 : _e.call(_d, (_f = flowdata[row_index]) === null || _f === void 0 ? void 0 : _f[col_index], {
171
171
  row: row_index,
172
172
  column: col_index,
@@ -91,11 +91,9 @@ function adjustFormulaForPaste(formula, srcCol, srcRow, destCol, destRow) {
91
91
  var rowOffset = destRow - srcRow;
92
92
  var hadInvalid = false;
93
93
  var cellRefRegex = /\b(\$?)([A-Z]+)(\$?)(\d+)\b/g;
94
- var stringOrCellRef = /"(?:\\.|[^"])*"|(?<!\$)([A-Z]+\d+\b)/g;
94
+ var stringOrCellRef = /"(?:\\.|[^"])*"|(\$?[A-Z]+\$?\d+)(?!\s*!)\b/g;
95
95
  var result = formula.replace(stringOrCellRef, function (m, cellRef) {
96
96
  if (!cellRef) return m;
97
- if (cellRef.startsWith("$")) return m;
98
- console.log(m, "cellRef", cellRef);
99
97
  return cellRef.replace(cellRefRegex, function (__, absCol, colLetters, absRow, rowNum) {
100
98
  var colIndex = columnLabelIndex(colLetters);
101
99
  var rowIndex = parseInt(rowNum, 10);
@@ -1024,9 +1022,10 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
1024
1022
  if (!_lodash.default.isNil(value) && !_lodash.default.isNil(value.f)) {
1025
1023
  var adjustedFormula = value.f;
1026
1024
  var isError = false;
1025
+ var srcRow = c_r1 + (h - mth);
1026
+ var srcCol = c_c1 + (c - mtc);
1027
1027
  try {
1028
- adjustedFormula = adjustFormulaForPaste(value.f, c_c1, c_r1, c, h);
1029
- console.log("adjustedFormula", adjustedFormula);
1028
+ adjustedFormula = adjustFormulaForPaste(value.f, srcCol, srcRow, c, h);
1030
1029
  } catch (error) {
1031
1030
  isError = true;
1032
1031
  value.error = {
@@ -1155,10 +1155,16 @@ function getInlineStringHTML(r, c, data) {
1155
1155
  var strObj = strings[i];
1156
1156
  if (strObj.v) {
1157
1157
  var style = getFontStyleByCell(strObj);
1158
+ var link = strObj.link;
1159
+ if ((link === null || link === void 0 ? void 0 : link.linkType) && (link === null || link === void 0 ? void 0 : link.linkAddress)) {
1160
+ style.color = style.color || "rgb(0, 0, 255)";
1161
+ style.borderBottom = style.borderBottom || "1px solid rgb(0, 0, 255)";
1162
+ }
1158
1163
  var styleStr = _lodash.default.map(style, function (v, key) {
1159
1164
  return "".concat(_lodash.default.kebabCase(key), ":").concat(_lodash.default.isNumber(v) ? "".concat(v, "px") : v, ";");
1160
1165
  }).join("");
1161
- value += "<span class=\"luckysheet-input-span\" index='".concat(i, "' style='").concat(styleStr, "'>").concat(strObj.v, "</span>");
1166
+ var dataAttrs = (link === null || link === void 0 ? void 0 : link.linkType) && (link === null || link === void 0 ? void 0 : link.linkAddress) ? " data-link-type='".concat(String(link.linkType).replace(/'/g, "&#39;"), "' data-link-address='").concat(String(link.linkAddress).replace(/'/g, "&#39;"), "'") : "";
1167
+ value += "<span class=\"luckysheet-input-span\" index='".concat(i, "' style='").concat(styleStr, "'").concat(dataAttrs, ">").concat(strObj.v, "</span>");
1162
1168
  }
1163
1169
  }
1164
1170
  return value;
@@ -1,3 +1,8 @@
1
1
  export declare function moveToEnd(obj: HTMLDivElement): void;
2
2
  export declare function selectTextContent(ele: HTMLElement): void;
3
3
  export declare function selectTextContentCross(sEle: HTMLElement, eEle: HTMLElement): void;
4
+ export declare function getSelectionCharacterOffsets(element: Node): {
5
+ start: number;
6
+ end: number;
7
+ } | null;
8
+ export declare function setSelectionByCharacterOffset(element: HTMLDivElement, start: number, end: number): void;
@@ -3,9 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.getSelectionCharacterOffsets = getSelectionCharacterOffsets;
6
7
  exports.moveToEnd = moveToEnd;
7
8
  exports.selectTextContent = selectTextContent;
8
9
  exports.selectTextContentCross = selectTextContentCross;
10
+ exports.setSelectionByCharacterOffset = setSelectionByCharacterOffset;
9
11
  function moveToEnd(obj) {
10
12
  if (document.createRange) {
11
13
  if (obj.innerHTML !== obj.innerText || obj.innerHTML === "") {
@@ -67,4 +69,59 @@ function selectTextContentCross(sEle, eEle) {
67
69
  }
68
70
  }
69
71
  }
72
+ }
73
+ function getSelectionCharacterOffsets(element) {
74
+ var sel = window.getSelection();
75
+ if (!sel || sel.rangeCount === 0) return null;
76
+ var range = sel.getRangeAt(0);
77
+ if (range.collapsed) return null;
78
+ if (!element.contains(range.startContainer) || !element.contains(range.endContainer)) {
79
+ return null;
80
+ }
81
+ var pre = document.createRange();
82
+ pre.selectNodeContents(element);
83
+ pre.setEnd(range.startContainer, range.startOffset);
84
+ var start = pre.toString().length;
85
+ return {
86
+ start: start,
87
+ end: start + range.toString().length
88
+ };
89
+ }
90
+ function setSelectionByCharacterOffset(element, start, end) {
91
+ element.focus();
92
+ var sel = window.getSelection();
93
+ if (!sel) return;
94
+ var charIndex = 0;
95
+ var startNode = null;
96
+ var startOffset = 0;
97
+ var endNode = null;
98
+ var endOffset = 0;
99
+ function walk(node) {
100
+ if (node.nodeType === Node.TEXT_NODE) {
101
+ var len = (node.textContent || "").length;
102
+ if (startNode == null && charIndex + len > start) {
103
+ startNode = node;
104
+ startOffset = start - charIndex;
105
+ }
106
+ if (endNode == null && charIndex + len >= end) {
107
+ endNode = node;
108
+ endOffset = end - charIndex;
109
+ return true;
110
+ }
111
+ charIndex += len;
112
+ return false;
113
+ }
114
+ for (var i = 0; i < node.childNodes.length; i += 1) {
115
+ if (walk(node.childNodes[i])) return true;
116
+ }
117
+ return false;
118
+ }
119
+ walk(element);
120
+ if (startNode && endNode) {
121
+ var range = document.createRange();
122
+ range.setStart(startNode, startOffset);
123
+ range.setEnd(endNode, endOffset);
124
+ sel.removeAllRanges();
125
+ sel.addRange(range);
126
+ }
70
127
  }
@@ -8,9 +8,19 @@ export declare function getCellHyperlink(ctx: Context, r: number, c: number): {
8
8
  linkType: string;
9
9
  linkAddress: string;
10
10
  } | undefined;
11
- export declare function saveHyperlink(ctx: Context, r: number, c: number, linkText: string, linkType: string, linkAddress: string): void;
11
+ export declare function saveHyperlink(ctx: Context, r: number, c: number, linkText: string, linkType: string, linkAddress: string, options?: {
12
+ applyToSelection?: boolean;
13
+ cellInput?: HTMLDivElement | null;
14
+ }): void;
12
15
  export declare function removeHyperlink(ctx: Context, r: number, c: number): void;
13
- export declare function showLinkCard(ctx: Context, r: number, c: number, isEditing?: boolean, isMouseDown?: boolean): void;
16
+ export declare function showLinkCard(ctx: Context, r: number, c: number, options?: {
17
+ applyToSelection?: boolean;
18
+ originText?: string;
19
+ selectionOffsets?: {
20
+ start: number;
21
+ end: number;
22
+ };
23
+ }, isEditing?: boolean, isMouseDown?: boolean): void;
14
24
  export declare function goToLink(ctx: Context, r: number, c: number, linkType: string, linkAddress: string, scrollbarX: HTMLDivElement, scrollbarY: HTMLDivElement): void;
15
25
  export declare function isLinkValid(ctx: Context, linkType: string, linkAddress: string): {
16
26
  isValid: boolean;
@@ -17,7 +17,9 @@ var _lodash = _interopRequireDefault(require("lodash"));
17
17
  var _context = require("../context");
18
18
  var _utils = require("../utils");
19
19
  var _cell = require("./cell");
20
+ var _cursor = require("./cursor");
20
21
  var _formula = require("./formula");
22
+ var _inlineString = require("./inline-string");
21
23
  var _location = require("./location");
22
24
  var _selection = require("./selection");
23
25
  var _sheet = require("./sheet");
@@ -54,9 +56,39 @@ function getCellHyperlink(ctx, r, c) {
54
56
  }
55
57
  return undefined;
56
58
  }
57
- function saveHyperlink(ctx, r, c, linkText, linkType, linkAddress) {
59
+ function saveHyperlink(ctx, r, c, linkText, linkType, linkAddress, options) {
60
+ var _a;
61
+ var applyToSelection = (options === null || options === void 0 ? void 0 : options.applyToSelection) && (options === null || options === void 0 ? void 0 : options.cellInput);
58
62
  var sheetIndex = (0, _utils.getSheetIndex)(ctx, ctx.currentSheetId);
59
63
  var flowdata = (0, _context.getFlowdata)(ctx);
64
+ if (applyToSelection) {
65
+ if (sheetIndex != null && flowdata != null && linkType && linkAddress) {
66
+ var cell = flowdata[r][c];
67
+ if (cell == null) cell = {};
68
+ _lodash.default.set(ctx.luckysheetfile[sheetIndex], ["hyperlink", "".concat(r, "_").concat(c)], {
69
+ linkType: linkType,
70
+ linkAddress: linkAddress
71
+ });
72
+ cell.v = linkText || linkAddress;
73
+ cell.m = linkText || linkAddress;
74
+ cell.hl = {
75
+ r: r,
76
+ c: c,
77
+ id: ctx.currentSheetId
78
+ };
79
+ flowdata[r][c] = cell;
80
+ }
81
+ var offsets = (_a = ctx.linkCard) === null || _a === void 0 ? void 0 : _a.selectionOffsets;
82
+ if (offsets) {
83
+ (0, _cursor.setSelectionByCharacterOffset)(options.cellInput, offsets.start, offsets.end);
84
+ }
85
+ (0, _inlineString.applyLinkToSelection)(options.cellInput, linkType, linkAddress);
86
+ ctx.linkCard = undefined;
87
+ return;
88
+ }
89
+ if (options === null || options === void 0 ? void 0 : options.cellInput) {
90
+ (0, _cell.cancelNormalSelected)(ctx);
91
+ }
60
92
  if (sheetIndex != null && flowdata != null && linkType && linkAddress) {
61
93
  var cell = flowdata[r][c];
62
94
  if (cell == null) cell = {};
@@ -95,8 +127,8 @@ function removeHyperlink(ctx, r, c) {
95
127
  }
96
128
  ctx.linkCard = undefined;
97
129
  }
98
- function showLinkCard(ctx, r, c, isEditing, isMouseDown) {
99
- var _a, _b, _c, _d, _e, _f, _g;
130
+ function showLinkCard(ctx, r, c, options, isEditing, isMouseDown) {
131
+ var _a, _b, _c, _d, _e, _f, _g, _h;
100
132
  if (isEditing === void 0) {
101
133
  isEditing = false;
102
134
  }
@@ -114,19 +146,26 @@ function showLinkCard(ctx, r, c, isEditing, isMouseDown) {
114
146
  if (isEditing || link != null && (!((_f = ctx.linkCard) === null || _f === void 0 ? void 0 : _f.isEditing) || isMouseDown) || ((_g = ctx.linkCard) === null || _g === void 0 ? void 0 : _g.sheetId) !== ctx.currentSheetId) {
115
147
  var col_pre = c - 1 === -1 ? 0 : ctx.visibledatacolumn[c - 1];
116
148
  var row = ctx.visibledatarow[r];
149
+ var originText = function () {
150
+ if ((options === null || options === void 0 ? void 0 : options.originText) !== undefined) return options.originText;
151
+ if ((cell === null || cell === void 0 ? void 0 : cell.v) == null) return "";
152
+ return "".concat(cell.v);
153
+ }();
117
154
  ctx.linkCard = {
118
155
  sheetId: ctx.currentSheetId,
119
156
  r: r,
120
157
  c: c,
121
158
  rc: "".concat(r, "_").concat(c),
122
- originText: (cell === null || cell === void 0 ? void 0 : cell.v) == null ? "" : "".concat(cell.v),
159
+ originText: originText,
123
160
  originType: (link === null || link === void 0 ? void 0 : link.linkType) || "webpage",
124
161
  originAddress: (link === null || link === void 0 ? void 0 : link.linkAddress) || "",
125
162
  position: {
126
163
  cellLeft: col_pre,
127
164
  cellBottom: row
128
165
  },
129
- isEditing: isEditing
166
+ isEditing: isEditing,
167
+ applyToSelection: (_h = options === null || options === void 0 ? void 0 : options.applyToSelection) !== null && _h !== void 0 ? _h : false,
168
+ selectionOffsets: options === null || options === void 0 ? void 0 : options.selectionOffsets
130
169
  };
131
170
  }
132
171
  }
@@ -31,6 +31,14 @@ export declare function isInlineStringCell(cell: any): boolean;
31
31
  export declare function isInlineStringCT(ct: any): boolean;
32
32
  export declare function getInlineStringNoStyle(r: number, c: number, data: CellMatrix): string;
33
33
  export declare function convertCssToStyleList(cssText: string, originCell: Cell): CellStyle;
34
- export declare function convertSpanToShareString($dom: NodeListOf<HTMLSpanElement>, originCell: Cell): CellStyle[];
34
+ export type InlineSegmentLink = {
35
+ linkType: string;
36
+ linkAddress: string;
37
+ };
38
+ export declare function convertSpanToShareString($dom: NodeListOf<HTMLSpanElement>, originCell: Cell): (CellStyle & {
39
+ v?: string | undefined;
40
+ link?: InlineSegmentLink | undefined;
41
+ })[];
35
42
  export declare function updateInlineStringFormatOutside(cell: Cell, key: string, value: any): void;
36
43
  export declare function updateInlineStringFormat(ctx: Context, attr: keyof Cell, value: any, cellInput: HTMLDivElement): void;
44
+ export declare function applyLinkToSelection(cellInput: HTMLDivElement, linkType: string, linkAddress: string): void;
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.applyLinkToSelection = applyLinkToSelection;
6
7
  exports.attrToCssName = void 0;
7
8
  exports.convertCssToStyleList = convertCssToStyleList;
8
9
  exports.convertSpanToShareString = convertSpanToShareString;
@@ -116,13 +117,20 @@ function convertCssToStyleList(cssText, originCell) {
116
117
  return styleList;
117
118
  }
118
119
  function convertSpanToShareString($dom, originCell) {
120
+ var _a, _b;
119
121
  var styles = [];
120
- var preStyleList;
122
+ var preStyleList = null;
121
123
  var preStyleListString = null;
122
124
  for (var i = 0; i < $dom.length; i += 1) {
123
125
  var span = $dom[i];
124
126
  var styleList = convertCssToStyleList(span.style.cssText, originCell);
125
- var curStyleListString = JSON.stringify(styleList);
127
+ if (((_a = span.dataset) === null || _a === void 0 ? void 0 : _a.linkType) && ((_b = span.dataset) === null || _b === void 0 ? void 0 : _b.linkAddress)) {
128
+ styleList.link = {
129
+ linkType: span.dataset.linkType,
130
+ linkAddress: span.dataset.linkAddress
131
+ };
132
+ }
133
+ var curStyleListString = JSON.stringify(_lodash.default.omit(styleList, "link"));
126
134
  var v = span.innerText;
127
135
  v = v.replace(/\n/g, "\r\n");
128
136
  if (i === $dom.length - 1) {
@@ -130,7 +138,7 @@ function convertSpanToShareString($dom, originCell) {
130
138
  v = v.slice(0, v.length - 2);
131
139
  }
132
140
  }
133
- if (curStyleListString === preStyleListString) {
141
+ if (curStyleListString === preStyleListString && _lodash.default.isEqual(preStyleList === null || preStyleList === void 0 ? void 0 : preStyleList.link, styleList.link)) {
134
142
  preStyleList.v += v;
135
143
  } else {
136
144
  styleList.v = v;
@@ -449,4 +457,128 @@ function updateInlineStringFormat(ctx, attr, value, cellInput) {
449
457
  }
450
458
  }
451
459
  }
460
+ }
461
+ function escapeHtmlAttr(s) {
462
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
463
+ }
464
+ function getLinkDataAttrs(span) {
465
+ var _a, _b;
466
+ if (((_a = span.dataset) === null || _a === void 0 ? void 0 : _a.linkType) && ((_b = span.dataset) === null || _b === void 0 ? void 0 : _b.linkAddress)) {
467
+ return " data-link-type='".concat(escapeHtmlAttr(span.dataset.linkType), "' data-link-address='").concat(escapeHtmlAttr(span.dataset.linkAddress), "'");
468
+ }
469
+ return "";
470
+ }
471
+ function getLinkStyleCssText(baseCssText) {
472
+ var css = getCssText(baseCssText, "fc", "rgb(0, 0, 255)");
473
+ css = getCssText(css, "un", 1);
474
+ return css;
475
+ }
476
+ function applyLinkToSelection(cellInput, linkType, linkAddress) {
477
+ var _a, _b, _c;
478
+ var w = window.getSelection();
479
+ if (!w || w.rangeCount === 0) return;
480
+ var range = w.getRangeAt(0);
481
+ if (range.collapsed) return;
482
+ var $textEditor = cellInput;
483
+ var endContainer = range.endContainer,
484
+ startContainer = range.startContainer,
485
+ endOffset = range.endOffset,
486
+ startOffset = range.startOffset;
487
+ if (startContainer === endContainer) {
488
+ var span = startContainer.parentNode;
489
+ var content = (span === null || span === void 0 ? void 0 : span.innerHTML) || "";
490
+ var fullContent = $textEditor.innerHTML;
491
+ var inherit = fullContent.substring(0, 5) !== "<span";
492
+ var s2 = startOffset;
493
+ var s3 = endOffset;
494
+ var left = content.substring(0, s2);
495
+ var mid = content.substring(s2, s3);
496
+ var right = content.substring(s3, content.length);
497
+ var cont = "";
498
+ var dataAttrs = getLinkDataAttrs(span);
499
+ if (left !== "") {
500
+ var cssText = span.style.cssText;
501
+ if (inherit) {
502
+ var box = span.closest("#luckysheet-input-box");
503
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
504
+ }
505
+ cont += "<span style='".concat(cssText, "'").concat(dataAttrs, ">").concat(left, "</span>");
506
+ }
507
+ if (mid !== "") {
508
+ var cssText = getLinkStyleCssText(span.style.cssText);
509
+ if (inherit) {
510
+ var box = span.closest("#luckysheet-input-box");
511
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
512
+ }
513
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(mid, "</span>");
514
+ }
515
+ if (right !== "") {
516
+ var cssText = span.style.cssText;
517
+ if (inherit) {
518
+ var box = span.closest("#luckysheet-input-box");
519
+ if (box != null) cssText = extendCssText(box.style.cssText, cssText);
520
+ }
521
+ cont += "<span style='".concat(cssText, "'").concat(dataAttrs, ">").concat(right, "</span>");
522
+ }
523
+ if (((_a = startContainer.parentElement) === null || _a === void 0 ? void 0 : _a.tagName) === "SPAN") {
524
+ span.outerHTML = cont;
525
+ } else {
526
+ span.innerHTML = cont;
527
+ }
528
+ var newSpans = $textEditor.querySelectorAll("span");
529
+ var linkSpanIndex = left === "" ? 0 : 1;
530
+ if (newSpans[linkSpanIndex]) {
531
+ (0, _cursor.selectTextContent)(newSpans[linkSpanIndex]);
532
+ }
533
+ } else if (((_b = startContainer.parentElement) === null || _b === void 0 ? void 0 : _b.tagName) === "SPAN" && ((_c = endContainer.parentElement) === null || _c === void 0 ? void 0 : _c.tagName) === "SPAN") {
534
+ var startSpan = startContainer.parentNode;
535
+ var endSpan = endContainer.parentNode;
536
+ var allSpans = $textEditor.querySelectorAll("span");
537
+ var startSpanIndex = _lodash.default.indexOf(allSpans, startSpan);
538
+ var endSpanIndex = _lodash.default.indexOf(allSpans, endSpan);
539
+ var startContent = (startSpan === null || startSpan === void 0 ? void 0 : startSpan.innerHTML) || "";
540
+ var endContent = (endSpan === null || endSpan === void 0 ? void 0 : endSpan.innerHTML) || "";
541
+ var s2 = startOffset;
542
+ var s3 = endOffset;
543
+ var sleft = startContent.substring(0, s2);
544
+ var sright = startContent.substring(s2, startContent.length);
545
+ var eleft = endContent.substring(0, s3);
546
+ var eright = endContent.substring(s3, endContent.length);
547
+ var spans = $textEditor.querySelectorAll("span");
548
+ var cont = "";
549
+ for (var i = 0; i < startSpanIndex; i += 1) {
550
+ var sp = spans[i];
551
+ cont += "<span style='".concat(sp.style.cssText, "'").concat(getLinkDataAttrs(sp), ">").concat(sp.innerHTML, "</span>");
552
+ }
553
+ if (sleft !== "") {
554
+ cont += "<span style='".concat(startSpan.style.cssText, "'").concat(getLinkDataAttrs(startSpan), ">").concat(sleft, "</span>");
555
+ }
556
+ if (sright !== "") {
557
+ var cssText = getLinkStyleCssText(startSpan.style.cssText);
558
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(sright, "</span>");
559
+ }
560
+ if (startSpanIndex < endSpanIndex) {
561
+ for (var i = startSpanIndex + 1; i < endSpanIndex; i += 1) {
562
+ var sp = spans[i];
563
+ var cssText = getLinkStyleCssText(sp.style.cssText);
564
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(sp.innerHTML, "</span>");
565
+ }
566
+ }
567
+ if (eleft !== "") {
568
+ var cssText = getLinkStyleCssText(endSpan.style.cssText);
569
+ cont += "<span style='".concat(cssText, "' data-link-type='").concat(escapeHtmlAttr(linkType), "' data-link-address='").concat(escapeHtmlAttr(linkAddress), "'>").concat(eleft, "</span>");
570
+ }
571
+ if (eright !== "") {
572
+ cont += "<span style='".concat(endSpan.style.cssText, "'").concat(getLinkDataAttrs(endSpan), ">").concat(eright, "</span>");
573
+ }
574
+ for (var i = endSpanIndex + 1; i < spans.length; i += 1) {
575
+ var sp = spans[i];
576
+ cont += "<span style='".concat(sp.style.cssText, "'").concat(getLinkDataAttrs(sp), ">").concat(sp.innerHTML, "</span>");
577
+ }
578
+ $textEditor.innerHTML = cont;
579
+ spans = $textEditor.querySelectorAll("span");
580
+ var startSel = sleft === "" ? startSpanIndex : startSpanIndex + 1;
581
+ var endSel = eright === "" ? endSpanIndex : endSpanIndex + 1;
582
+ (0, _cursor.selectTextContentCross)(spans[startSel], spans[endSel]);
583
+ }
452
584
  }
@@ -26,7 +26,7 @@ export declare function handleSort(ctx: Context, isAsc: boolean): void;
26
26
  export declare function handleFreeze(ctx: Context, type: string): void;
27
27
  export declare function handleTextSize(ctx: Context, cellInput: HTMLDivElement, size: number, canvas?: CanvasRenderingContext2D): void;
28
28
  export declare function handleSum(ctx: Context, cellInput: HTMLDivElement, fxInput: HTMLDivElement | null | undefined, cache?: GlobalCache): void;
29
- export declare function handleLink(ctx: Context): void;
29
+ export declare function handleLink(ctx: Context, cellInput: HTMLDivElement | null | undefined): void;
30
30
  export declare function toolbarItemClickHandler(name: string): ToolbarItemClickHandler;
31
31
  export declare function toolbarItemSelectedFunc(name: string): ToolbarItemSelectedFunc;
32
32
  export {};
@@ -37,6 +37,7 @@ var _utils = require("../utils");
37
37
  var _cell = require("./cell");
38
38
  var _color = require("./color");
39
39
  var _format = require("./format");
40
+ var _cursor = require("./cursor");
40
41
  var _formula2 = require("./formula");
41
42
  var _inlineString = require("./inline-string");
42
43
  var _location = require("./location");
@@ -1002,15 +1003,36 @@ function handleTextSize(ctx, cellInput, size, canvas) {
1002
1003
  function handleSum(ctx, cellInput, fxInput, cache) {
1003
1004
  autoSelectionFormula(ctx, cellInput, fxInput, "SUM", cache);
1004
1005
  }
1005
- function handleLink(ctx) {
1006
- var _a;
1006
+ function handleLink(ctx, cellInput) {
1007
+ var _a, _b, _c;
1007
1008
  var allowEdit = (0, _utils.isAllowEdit)(ctx);
1008
1009
  if (!allowEdit) return;
1009
1010
  var selection = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[0];
1010
1011
  var flowdata = (0, _context.getFlowdata)(ctx);
1011
- if (flowdata != null && selection != null) {
1012
- (0, _hyperlink.showLinkCard)(ctx, selection.row[0], selection.column[0], true);
1012
+ if (flowdata == null || selection == null) return;
1013
+ var r = selection.row[0];
1014
+ var c = selection.column[0];
1015
+ var isEditMode = ((_b = ctx.luckysheetCellUpdate) === null || _b === void 0 ? void 0 : _b.length) === 2 && ctx.luckysheetCellUpdate[0] === r && ctx.luckysheetCellUpdate[1] === c;
1016
+ var applyToSelection = false;
1017
+ var originText;
1018
+ var selectionOffsets;
1019
+ if (isEditMode && cellInput) {
1020
+ var sel = window.getSelection();
1021
+ if (sel && sel.rangeCount > 0 && !sel.getRangeAt(0).collapsed && cellInput.contains(sel.anchorNode)) {
1022
+ var value = (_c = cellInput.innerText) !== null && _c !== void 0 ? _c : "";
1023
+ if (value.substring(0, 1) !== "=") {
1024
+ originText = sel.toString();
1025
+ applyToSelection = true;
1026
+ var off = (0, _cursor.getSelectionCharacterOffsets)(cellInput);
1027
+ if (off) selectionOffsets = off;
1028
+ }
1029
+ }
1013
1030
  }
1031
+ (0, _hyperlink.showLinkCard)(ctx, r, c, {
1032
+ applyToSelection: applyToSelection || undefined,
1033
+ originText: originText,
1034
+ selectionOffsets: selectionOffsets
1035
+ }, true, false);
1014
1036
  }
1015
1037
  var handlerMap = {
1016
1038
  "currency-format": handleCurrencyFormat,
package/lib/types.d.ts CHANGED
@@ -269,6 +269,11 @@ export type LinkCardProps = {
269
269
  };
270
270
  isEditing: boolean;
271
271
  selectingCellRange?: boolean;
272
+ applyToSelection?: boolean;
273
+ selectionOffsets?: {
274
+ start: number;
275
+ end: number;
276
+ };
272
277
  };
273
278
  export type RangeDialogProps = {
274
279
  show: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-core",
3
- "version": "1.3.4-viewMode-5",
3
+ "version": "1.3.5-hyper",
4
4
  "main": "lib/index.js",
5
5
  "module": "es/index.js",
6
6
  "typings": "lib/index.d.ts",