Skip to content

Commit

Permalink
Merge pull request #19 from xmindltd/feat/load_from_zip
Browse files Browse the repository at this point in the history
merge feature
  • Loading branch information
danielsss authored Jun 3, 2020
2 parents 49ab66c + 64a49d2 commit eda706a
Show file tree
Hide file tree
Showing 22 changed files with 1,261 additions and 914 deletions.
1 change: 1 addition & 0 deletions .githooks/pre-commit/filter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ npm run lint

if [[ $? != 0 ]]; then
echo "lint error."

exit 1;
fi

Expand Down
2 changes: 2 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"src/common",
"src/index.ts",
"src/browser.ts",
"src/snowbrush.js",
"src/snowbrush.js.map",
"test/",
"example/",
"dist/",
Expand Down
62 changes: 50 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const {Workbook, Topic, Marker} = require('xmind');
### Usage in Browser

```jsx harmony
import {Workbook, Topic, Marker} from 'xmind';
import { Workbook, Topic, Marker, Loader, Dumper } from 'xmind';
```

```html
Expand All @@ -52,7 +52,7 @@ import {Workbook, Topic, Marker} from 'xmind';
<!-- script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/xmind-sdk.bundle.js"></script -->

<script>
const { Workbook, Topic, Marker } = window;
const { Workbook, Topic, Marker, Loader, Dumper } = window;
</script>

```
Expand Down Expand Up @@ -95,8 +95,41 @@ zipper.save().then(status => status && console.log('Saved /tmp/MyFirstMap.xmind'

See [example directory](./example).

## Loader

### Workbook
The loader helps you loading an exists .xmind file into `sdk`.

### Options

* options.ctx: `JSZip`

> Usage:
>
> ```js
> const { Loader, Topic } = require('xmind');
> const JSZip = require('jszip');
>
> const main = async () => {
> const loader = new Loader({ctx: await JSZip.loadAsync('/absolute/path/file.xmind')});
> const sheets = await loader.loadSheets();
> /* {[id: string]: Sheet} */
> const topic = new Topic({sheet: sheets[0], isLoaded: true});
> }
> ```
>
> [See fully example](./example/example.loader.js)
### Methods
#### .loadSheets() => Promise<{[id: string]: Sheet}>
It returns an array of object which is used to get topic instance as parameter.
#### .getWorkbook() => Workbook
The Loader will create a workbook instance automatically. It is useful if you want to save the map via `Zipper`.
## Workbook
The workbook is a temporary storage where all the data are written.
Expand All @@ -113,6 +146,10 @@ Once the workbook is created, then there's a way to build a sheet containing a `
| topicTitle | String | `Central Topic` | N |
#### .loadSheets(sheets: SheetData[]) => Promise<{[id: string]: Sheet}>
This method can help you to load sheets from an exists `.xmind` file easily, But we encourage you to load sheets starting with `Loader`.
#### .theme(sheetTitle, themeName) => Boolean
The `UI client` has many theme styles and this library also offers some of them, such as `robust / snowbrush / business`.
Expand All @@ -136,13 +173,14 @@ This is proof that all data are available and complete.
The `status` indicates the result of validation which is `true` if it's correct, othewise `false` returns.
### Topic
## Topic
The `Topic` is an important constructor function that implements most of the methods. And you are going to depend on it during most operations.
### Topic Options
* options.sheet <= `workbook.createSheet(...)`
* options.sheet: `workbook.createSheet(...)`
* options.isLoaded: Set true if you starts with `Loader`
You may wonder why we need to offer the `options.sheet` manually? The reason is that `Topic` is implemented independently and most of the methods depend on the instance of the sheet.
Expand Down Expand Up @@ -174,7 +212,7 @@ If you don't specify title in the period of calling .cid, the last added compone
#### .cids() => {$cid: $title}
That will return all added components.
That will return all added components which is not included `note` or `marker`.
#### .add(options) => Topic
Expand Down Expand Up @@ -233,7 +271,7 @@ Attach a summary component to parent node including all children. In the meantim
| Name | Type | Default | Required |
|:---- |:----:|:-------:|:--------:|
| options.title | String | null | Y |
| options.edge | String | null | N |
| options.edge | String | null | N |


> [About `edge`](./docs/edge.graphic.txt)
Expand All @@ -248,7 +286,7 @@ Destroy a component from the map tree.
> All children would be destroyed along with it

### Marker flags
## Marker flags

We provide an instance of `Marker` that includes all the markers. Such as:

Expand Down Expand Up @@ -289,18 +327,18 @@ List available group names.

#### Marker.names(groupName) => Array\<name\>

* Get the flag names by `groupName`.
Get the flag names by `groupName`.


### Zipper
## Zipper

The module of `Zipper` only works under backend.

> [!See `Dumper` in browser environment](#dumper)
### Zipper Options

| Name | Type | Default | Required | Description |
| Name | Type | Default | Required | Description |
|:---- |:----:|:-------:|:--------:|:------------|
| options.path | String | `-` | Y | The path is where to save the `.xmind` file |
| options.workbook | Workbook | `-` | Y | The instance of Workbook |
Expand All @@ -310,7 +348,7 @@ The module of `Zipper` only works under backend.

Update manifest for image insertion.

| Name | Type | Default | Required | Description |
| Name | Type | Default | Required | Description |
|:---- |:----:|:-------:|:--------:|:------------|
| key | String | null | Y | The key only can get by topic.image() |
| content | Buffer | null | Y | The buffer data of image |
Expand Down
41 changes: 41 additions & 0 deletions example/example.loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

/**
* Loader fully example
*/

const { Loader, Topic, Zipper } = require('xmind');
const JSZip = require('jszip');
const fs = require('fs');
const path = require('path');
const v4 = require('uuid').v4;

const main = async () => {
const file = fs.readFileSync(path.resolve(__dirname, '../test/fixtures/default.xmind'));
const loader = new Loader({ctx: await JSZip.loadAsync(file)});
const sheets = await loader.loadSheets();

let sheet;
for (const key in sheets) {
if (sheets[key].getTitle() === 'sheet-1') {
sheet = sheets[key];
}
}

const topic = new Topic({sheet: sheet, isLoaded: true});

const resource = topic.cids();
for (const topicId in resource) {
console.info(resource[topicId]);
if (resource[topicId] === 'Computer science') {
topic
.on(topicId)
.add({id: v4(), title: 'New Computer Science'})
}
}

const zip = new Zipper({ path: '/tmp', workbook: loader.getWorkbook() });
return zip.save().then(status => status && console.info('saved!'));
};

main();
Loading

0 comments on commit eda706a

Please sign in to comment.