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

fix: add image #26

Merged
merged 1 commit into from
Nov 3, 2023
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
156 changes: 78 additions & 78 deletions lib/doc/anchor.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
// 'use strict';
'use strict';

// const colCache = require('../utils/col-cache');
const colCache = require('../utils/col-cache');

// class Anchor {
// constructor(worksheet, address, offset = 0) {
// if (!address) {
// this.nativeCol = 0;
// this.nativeColOff = 0;
// this.nativeRow = 0;
// this.nativeRowOff = 0;
// } else if (typeof address === 'string') {
// const decoded = colCache.decodeAddress(address);
// this.nativeCol = decoded.col + offset;
// this.nativeColOff = 0;
// this.nativeRow = decoded.row + offset;
// this.nativeRowOff = 0;
// } else if (address.nativeCol !== undefined) {
// this.nativeCol = address.nativeCol || 0;
// this.nativeColOff = address.nativeColOff || 0;
// this.nativeRow = address.nativeRow || 0;
// this.nativeRowOff = address.nativeRowOff || 0;
// } else if (address.col !== undefined) {
// this.col = address.col + offset;
// this.row = address.row + offset;
// } else {
// this.nativeCol = 0;
// this.nativeColOff = 0;
// this.nativeRow = 0;
// this.nativeRowOff = 0;
// }
class Anchor {
constructor(worksheet, address, offset = 0) {
if (!address) {
this.nativeCol = 0;
this.nativeColOff = 0;
this.nativeRow = 0;
this.nativeRowOff = 0;
} else if (typeof address === 'string') {
const decoded = colCache.decodeAddress(address);
this.nativeCol = decoded.col + offset;
this.nativeColOff = 0;
this.nativeRow = decoded.row + offset;
this.nativeRowOff = 0;
} else if (address.nativeCol !== undefined) {
this.nativeCol = address.nativeCol || 0;
this.nativeColOff = address.nativeColOff || 0;
this.nativeRow = address.nativeRow || 0;
this.nativeRowOff = address.nativeRowOff || 0;
} else if (address.col !== undefined) {
this.col = address.col + offset;
this.row = address.row + offset;
} else {
this.nativeCol = 0;
this.nativeColOff = 0;
this.nativeRow = 0;
this.nativeRowOff = 0;
}

// this.worksheet = worksheet;
// }
this.worksheet = worksheet;
}

// static asInstance(model) {
// return model instanceof Anchor || model == null ? model : new Anchor(model);
// }
static asInstance(model) {
return model instanceof Anchor || model == null ? model : new Anchor(model);
}

// get col() {
// return this.nativeCol + (Math.min(this.colWidth - 1, this.nativeColOff) / this.colWidth);
// }
get col() {
return this.nativeCol + (Math.min(this.colWidth - 1, this.nativeColOff) / this.colWidth);
}

// set col(v) {
// this.nativeCol = Math.floor(v);
// this.nativeColOff = Math.floor((v - this.nativeCol) * this.colWidth);
// }
set col(v) {
this.nativeCol = Math.floor(v);
this.nativeColOff = Math.floor((v - this.nativeCol) * this.colWidth);
}

// get row() {
// return this.nativeRow + (Math.min(this.rowHeight - 1, this.nativeRowOff) / this.rowHeight);
// }
get row() {
return this.nativeRow + (Math.min(this.rowHeight - 1, this.nativeRowOff) / this.rowHeight);
}

// set row(v) {
// this.nativeRow = Math.floor(v);
// this.nativeRowOff = Math.floor((v - this.nativeRow) * this.rowHeight);
// }
set row(v) {
this.nativeRow = Math.floor(v);
this.nativeRowOff = Math.floor((v - this.nativeRow) * this.rowHeight);
}

// get colWidth() {
// return this.worksheet &&
// this.worksheet.getColumn(this.nativeCol + 1) &&
// this.worksheet.getColumn(this.nativeCol + 1).isCustomWidth
// ? Math.floor(this.worksheet.getColumn(this.nativeCol + 1).width * 10000)
// : 640000;
// }
get colWidth() {
return this.worksheet &&
this.worksheet.getColumn(this.nativeCol + 1) &&
this.worksheet.getColumn(this.nativeCol + 1).isCustomWidth
? Math.floor(this.worksheet.getColumn(this.nativeCol + 1).width * 10000)
: 640000;
}

// get rowHeight() {
// return this.worksheet &&
// this.worksheet.getRow(this.nativeRow + 1) &&
// this.worksheet.getRow(this.nativeRow + 1).height
// ? Math.floor(this.worksheet.getRow(this.nativeRow + 1).height * 10000)
// : 180000;
// }
get rowHeight() {
return this.worksheet &&
this.worksheet.getRow(this.nativeRow + 1) &&
this.worksheet.getRow(this.nativeRow + 1).height
? Math.floor(this.worksheet.getRow(this.nativeRow + 1).height * 10000)
: 180000;
}

// get model() {
// return {
// nativeCol: this.nativeCol,
// nativeColOff: this.nativeColOff,
// nativeRow: this.nativeRow,
// nativeRowOff: this.nativeRowOff,
// };
// }
get model() {
return {
nativeCol: this.nativeCol,
nativeColOff: this.nativeColOff,
nativeRow: this.nativeRow,
nativeRowOff: this.nativeRowOff,
};
}

// set model(value) {
// this.nativeCol = value.nativeCol;
// this.nativeColOff = value.nativeColOff;
// this.nativeRow = value.nativeRow;
// this.nativeRowOff = value.nativeRowOff;
// }
// }
set model(value) {
this.nativeCol = value.nativeCol;
this.nativeColOff = value.nativeColOff;
this.nativeRow = value.nativeRow;
this.nativeRowOff = value.nativeRowOff;
}
}

// module.exports = Anchor;
module.exports = Anchor;
108 changes: 54 additions & 54 deletions lib/doc/image.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
// const colCache = require('../utils/col-cache');
// const Anchor = require('./anchor');
const colCache = require('../utils/col-cache');
const Anchor = require('./anchor');

// class Image {
// constructor(worksheet, model) {
// this.worksheet = worksheet;
// this.model = model;
// }
class Image {
constructor(worksheet, model) {
this.worksheet = worksheet;
this.model = model;
}

// get model() {
// switch (this.type) {
// case 'background':
// return {
// type: this.type,
// imageId: this.imageId,
// };
// case 'image':
// return {
// type: this.type,
// imageId: this.imageId,
// hyperlinks: this.range.hyperlinks,
// range: {
// tl: this.range.tl.model,
// br: this.range.br && this.range.br.model,
// ext: this.range.ext,
// editAs: this.range.editAs,
// },
// };
// default:
// throw new Error('Invalid Image Type');
// }
// }
get model() {
switch (this.type) {
case 'background':
return {
type: this.type,
imageId: this.imageId,
};
case 'image':
return {
type: this.type,
imageId: this.imageId,
hyperlinks: this.range.hyperlinks,
range: {
tl: this.range.tl.model,
br: this.range.br && this.range.br.model,
ext: this.range.ext,
editAs: this.range.editAs,
},
};
default:
throw new Error('Invalid Image Type');
}
}

// set model({type, imageId, range, hyperlinks}) {
// this.type = type;
// this.imageId = imageId;
set model({type, imageId, range, hyperlinks}) {
this.type = type;
this.imageId = imageId;

// if (type === 'image') {
// if (typeof range === 'string') {
// const decoded = colCache.decode(range);
// this.range = {
// tl: new Anchor(this.worksheet, {col: decoded.left, row: decoded.top}, -1),
// br: new Anchor(this.worksheet, {col: decoded.right, row: decoded.bottom}, 0),
// editAs: 'oneCell',
// };
// } else {
// this.range = {
// tl: new Anchor(this.worksheet, range.tl, 0),
// br: range.br && new Anchor(this.worksheet, range.br, 0),
// ext: range.ext,
// editAs: range.editAs,
// hyperlinks: hyperlinks || range.hyperlinks,
// };
// }
// }
// }
// }
if (type === 'image') {
if (typeof range === 'string') {
const decoded = colCache.decode(range);
this.range = {
tl: new Anchor(this.worksheet, {col: decoded.left, row: decoded.top}, -1),
br: new Anchor(this.worksheet, {col: decoded.right, row: decoded.bottom}, 0),
editAs: 'oneCell',
};
} else {
this.range = {
tl: new Anchor(this.worksheet, range.tl, 0),
br: range.br && new Anchor(this.worksheet, range.br, 0),
ext: range.ext,
editAs: range.editAs,
hyperlinks: hyperlinks || range.hyperlinks,
};
}
}
}
}

// module.exports = Image;
module.exports = Image;
24 changes: 12 additions & 12 deletions lib/doc/workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Workbook {
this.subject = '';
this.title = '';
// this.views = [];
// this.media = [];
this.media = [];
// this._definedNames = new DefinedNames();
}

Expand Down Expand Up @@ -153,16 +153,16 @@ class Workbook {
// this._themes = undefined;
// }

// addImage(image) {
// // TODO: validation?
// const id = this.media.length;
// this.media.push(Object.assign({}, image, {type: 'image'}));
// return id;
// }
addImage(image) {
// TODO: validation?
const id = this.media.length;
this.media.push(Object.assign({}, image, {type: 'image'}));
return id;
}

// getImage(id) {
// return this.media[id];
// }
getImage(id) {
return this.media[id];
}

get model() {
return {
Expand All @@ -187,7 +187,7 @@ class Workbook {
revision: this.revision,
contentStatus: this.contentStatus,
// themes: this._themes,
// media: this.media,
media: this.media,
calcProperties: this.calcProperties,
};
}
Expand Down Expand Up @@ -229,7 +229,7 @@ class Workbook {
// this._definedNames.model = value.definedNames;
// this.views = value.views;
// this._themes = value.themes;
// this.media = value.media || [];
this.media = value.media || [];
}
}

Expand Down
Loading
Loading