Skip to content

Commit

Permalink
Merge pull request #74 from xmindltd/d/characters
Browse files Browse the repository at this point in the history
Fixed(#69): character issues
  • Loading branch information
danielsss authored Jun 14, 2023
2 parents c6e7314 + d5e87ea commit 1fb9379
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 53 deletions.
17 changes: 10 additions & 7 deletions example/example.fully.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const createdSheetList = workbook2.createSheets([
{s: 'sheetName3', t: 'rootTopicName3'}
]);

console.info(createdSheetList);
console.info('sheetList:', workbook2.getSheets());
console.info('sheet1', workbook2.getSheet(createdSheetList[0].id));
// console.info(createdSheetList);
// console.info('sheetList:', workbook2.getSheets());
// console.info('sheet1', workbook2.getSheet(createdSheetList[0].id));

const zip2 = new Zipper({path: '/tmp', workbook: workbook2});
// const zip2 = new Zipper({path: '/tmp', workbook: workbook2});

// Set theme
workbook.theme('sheet-1', 'robust');
Expand All @@ -48,6 +48,7 @@ topic
.add({title: 'C++'})
.add({title: '中文测试'})
.add({title: 'にほんご/にっぽんご'})
.add({title: 'mixed123中文ぽんご😋'})
.add({title: 'Java'})
.on(topic.cid('C'))
.summary({title: 'Low level that is hard to learning', edge: topic.cid('C++')})
Expand All @@ -73,13 +74,15 @@ topic
.add({title: 'CLion'})
.add({title: 'IntelliJ Idea'})
.add({title: 'etc.'})
.summary({title: 'all of the productions belongs to jetbrains'})
.summary({title: 'all of the productions are belonging to jetbrains'})

.on(topic.cid('Microsoft'))
.marker(marker.smiley('cry'))
.add({title: 'vs code'});

// console.info(workbook.toJSON());

zip.save().then(status => status && console.log('zip saved'));
zip2.save().then(status => status && console.log('zip2 saved'));
zip.save().then(status => {
status && console.log('%s saved', zip.target());
});
// zip2.save().then(status => status && console.log('zip2 saved'));
52 changes: 36 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"ajv": "^6.10.0",
"debug": "^4.1.1",
"iconv-lite": "^0.6.3",
"jszip": "^3.2.1",
"tree-model": "^1.0.7",
"uuid": "^3.3.2",
Expand Down
29 changes: 10 additions & 19 deletions src/utils/zipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PACKAGE_MAP } from '../common/constants';
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const iconv = require('iconv-lite');

/* istanbul ignore next */
const join = (process.platform === 'win32' ? path.win32.join : path.join);
Expand Down Expand Up @@ -47,14 +48,17 @@ export class Zipper extends Base {
};
}

public target() {
return join(this.path, this.filename);
}

/**
* @description Saving zip file
* @return { Promise }
*/
public async save() {
if (this.workbook) {
const contents = Buffer.from(this.workbook.toString(), 'utf-8').toString();
this.addJSONContent(contents);
this.addJSONContent(this.workbook.toString());
this.addMetadataContents();
this.addXMLContent();
this.addManifestContents();
Expand All @@ -63,8 +67,7 @@ export class Zipper extends Base {
const options: JSZip.JSZipGeneratorOptions = {
type: 'nodebuffer',
compression: 'STORE',
//compressionOptions: {level: 9},
platform: 'UNIX'
compressionOptions: { level: 9 }
};

const metadata = await this.zip.generateAsync(options);
Expand All @@ -87,19 +90,7 @@ export class Zipper extends Base {
}
const arr = key.split('/');
this.manifest['file-entries'][key] = {};
this.zip.folder(arr[0]).file(arr[1], content, {binary: true});
return this;
}

/**
* @description Remove metadata from manifest
* @param {String} key - the file key that was already stored in metadata of manifest
* @return {Zipper}
*/
public removeManifestMetadata(key: string) {
if (!key) return this;
this.zip.remove(key);
delete this.manifest['file-entries'][key];
this.zip.folder(arr[0]).file(arr[1], content, { binary: false });
return this;
}

Expand Down Expand Up @@ -127,7 +118,7 @@ export class Zipper extends Base {
if (isObject(contents)) {
contents = JSON.stringify(contents);
}
this.zip.file(PACKAGE_MAP.CONTENT_JSON.NAME, contents);
this.zip.file(PACKAGE_MAP.CONTENT_JSON.NAME, iconv.decode(Buffer.from(contents), 'utf8'));
return this;
}

Expand All @@ -136,7 +127,7 @@ export class Zipper extends Base {
*/
private addXMLContent() {
const p = join(__dirname, '../common/templates/content.xml');
this.zip.file(PACKAGE_MAP.CONTENT_XML.NAME, fs.readFileSync(p));
this.zip.file(PACKAGE_MAP.CONTENT_XML.NAME, iconv.decode(fs.readFileSync(p), 'utf8'));
return this;
}
}
Expand Down
65 changes: 55 additions & 10 deletions test/units/topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const getComponents = function() {
const workbook = new Workbook();
const topic = new Topic({sheet: workbook.createSheet('sheet1', 'centralTopic')});
const zip = new Zipper({path: getBuildTemporaryPath(), workbook});
return {topic, workbook, zip};
const marker = new Marker();
return {topic, workbook, zip, marker };
}

describe('# Topic Unit Test', () => {
Expand Down Expand Up @@ -129,15 +130,59 @@ describe('# Topic Unit Test', () => {
});

it('the component of summary should be created if does not given options', done => {
const {topic, zip} = getComponents();
topic
.on(topic.rootTopicId)
.add({title: 'main topic 1'})
.add({title: 'main topic 2'})
.on(topic.cid('main topic 1'))
.summary();

zip.save().then(status => status && done());
const {topic, zip, marker } = getComponents();
topic.add({title: 'Programming Language'})
.add({title: 'Software Name'})
.add({title: 'Network device'})
.add({title: 'Computer Brand'})
.marker(marker.smiley('smile'))


.on(topic.cid('Programming Language'))
.add({title: 'dynamic'})
.add({title: 'static'})

.on(topic.cid()/* Also the topic.cid('static') is working */)
.add({title: 'C'})
.add({title: 'C++'})
.add({title: '中文测试'})
.add({title: 'にほんご/にっぽんご'})
.add({title: 'mixed123中文ぽんご😋'})
.add({title: 'Java'})
.on(topic.cid('C'))
.summary({title: 'Low level that is hard to learning', edge: topic.cid('C++')})

.on(topic.cid('dynamic'))
.note('The static languages are fast more than dynamic language')
.add({title: 'Node.js'})
.add({title: 'Python'})
.add({title: 'Ruby'})
.on(topic.cid('dynamic'))
.summary({title: 'In popular'})


// on Software
.on(topic.cid('Software'))
.add({title: 'jetBrains'})
.add({title: 'Microsoft'})

.on(topic.cid('jetBrains'))
.marker(marker.smiley('smile'))
.add({title: 'WebStorm'})
.add({title: 'Pycharm'})
.add({title: 'CLion'})
.add({title: 'IntelliJ Idea'})
.add({title: 'etc.'})
.summary({title: 'all of the productions are belonging to jetbrains'})

.on(topic.cid('Microsoft'))
.marker(marker.smiley('cry'))
.add({title: 'vs code'});

zip.save().then(status => {
status && done()
console.info('==========', zip.target())
});
});

it('should return the central topic id if never to add component', done => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"compilerOptions": {
"baseUrl": ".",
"paths": { "*": ["types/*"] },
"target": "es5",
"target": "es6",
"module": "CommonJS",
"moduleResolution": "node",
"experimentalDecorators": true,
Expand Down

0 comments on commit 1fb9379

Please sign in to comment.