Skip to content

Commit

Permalink
Merge pull request #481 from ruilisi/inline-text-style
Browse files Browse the repository at this point in the history
fix: style loss while adding style to inline text
  • Loading branch information
sanchit3008 authored Mar 20, 2024
2 parents ec39a72 + d3e8fba commit fdcb8b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/modules/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,18 +746,21 @@ export function updateCell(
}
curv ||= {};
const fontSize = curv.fs || 10;
delete curv.fs;
delete curv.f;
delete curv.v;
delete curv.m;

if (!curv.ct) {
curv.ct = {};
curv.ct.fa = "General";
}

curv.ct.t = "inlineStr";
curv.ct.s = convertSpanToShareString($input!.querySelectorAll("span"));
curv.ct.s = convertSpanToShareString(
$input!.querySelectorAll("span"),
curv
);
delete curv.fs;
delete curv.f;
delete curv.v;
delete curv.m;
curv.fs = fontSize;
if (isCopyVal) {
curv.ct.s = [
Expand Down
30 changes: 16 additions & 14 deletions packages/core/src/modules/inline-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ export function getInlineStringNoStyle(r: number, c: number, data: CellMatrix) {
return "";
}

export function convertCssToStyleList(cssText: string) {
export function convertCssToStyleList(cssText: string, originCell: Cell) {
if (_.isEmpty(cssText)) {
return {};
}
const cssTextArray = cssText.split(";");

const styleList: CellStyle = {
// ff: locale_fontarray[0], // font family
fc: "#000000", // font color
fs: 10, // font size
cl: 0, // strike
un: 0, // underline
bl: 0, // blod
it: 0, // italic
fc: originCell.fc || "#000000", // font color
fs: originCell.fs || 10, // font size
cl: originCell.cl || 0, // strike
un: originCell.un || 0, // underline
bl: originCell.bl || 0, // blod
it: originCell.it || 0, // italic
};
cssTextArray.forEach((s) => {
s = s.toLowerCase();
Expand All @@ -80,16 +80,12 @@ export function convertCssToStyleList(cssText: string) {
if (key === "font-weight") {
if (value === "bold") {
styleList.bl = 1;
} else {
styleList.bl = 0;
}
}

if (key === "font-style") {
if (value === "italic") {
styleList.it = 1;
} else {
styleList.it = 0;
}
}

Expand Down Expand Up @@ -130,14 +126,20 @@ export function convertCssToStyleList(cssText: string) {
return styleList;
}

// eslint-disable-next-line no-undef
export function convertSpanToShareString($dom: NodeListOf<HTMLSpanElement>) {
export function convertSpanToShareString(
// eslint-disable-next-line no-undef
$dom: NodeListOf<HTMLSpanElement>,
originCell: Cell
) {
const styles: CellStyle[] = [];
let preStyleList: Cell;
let preStyleListString = null;
for (let i = 0; i < $dom.length; i += 1) {
const span = $dom[i];
const styleList = convertCssToStyleList(span.style.cssText) as Cell;
const styleList = convertCssToStyleList(
span.style.cssText,
originCell
) as Cell;

const curStyleListString = JSON.stringify(styleList);
// let v = span.innerHTML;
Expand Down

0 comments on commit fdcb8b0

Please sign in to comment.