Skip to content

Commit

Permalink
+ add shrinkToFit
Browse files Browse the repository at this point in the history
~ a bit more code cleanup
  • Loading branch information
joethei committed Feb 5, 2022
1 parent b001fea commit 52dd7c4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 94 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -65,7 +65,8 @@ The following options are supported for all clouds.
| shape | What shape to draw | `circle`, `cardioid`, `diamond`, `square`, `triangle-forward`, `triangle`, `pentagon`, `star` | `circle` |
| source | where are the tags/words coming from? | `file`, `vault`, `query`(only supported in tagcloud) | `vault` |
| weight | factor by wich the size of a word is multiplied | any positive number | `2` |
| minCount | Minumum number of occurances | any positive number | '0' |
| shrinkToFit | Adjust word weight to make it fit | `true`/`false` | `true` |
| minCount | Minumum number of occurances | any positive number | `0` |
| background | Background color | a hexadecimal RGB value | background color from the chosen theme |
| width | Width of canvas | in pixels, (the `px` is omitted) | line width |
| height | Height of canvas | in pixels, (the `px` is omitted) | `width / 2` |
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
@@ -1,7 +1,7 @@
{
"id": "tag-word-cloud",
"name": "Tag & Word Cloud",
"version": "1.2.1",
"version": "1.2.2",
"minAppVersion": "0.12.0",
"description": "Show a cloud of your tags/words in a note",
"author": "Johannes Theiner",
Expand Down
28 changes: 1 addition & 27 deletions src/linkcloud.ts
@@ -1,6 +1,5 @@
import {MarkdownPostProcessorContext} from "obsidian";
import TagCloudPlugin from "./main";
import WordCloud from "wordcloud";

export class LinkCloud {
plugin: TagCloudPlugin;
Expand Down Expand Up @@ -62,31 +61,6 @@ export class LinkCloud {
return;
}

const canvas = el.createEl('canvas', {attr: {id: "tagcloud"}});
canvas.width = options.width;
canvas.height = options.height;

//@ts-ignore
const searchPlugin = this.plugin.app.internalPlugins.getPluginById("global-search");
const search = searchPlugin && searchPlugin.instance;

WordCloud(canvas, {
list: Array.from(finalMap.entries()),
backgroundColor: options.backgroundColor,
color: options.color,
shape: options.shape,
weightFactor: options.weightFactor,
fontFamily: options.fontFamily,
fontWeight: options.fontWeight,
minSize: options.minFontSize,
minRotation: options.minRotation,
maxRotation: options.maxRotation,
ellipticity: options.ellipticity,
shuffle: options.shuffle,
rotateRatio: options.rotateRatio,
click: item => {
search.openGlobalSearch("file:" + item[0]);
},
});
this.plugin.generateCloud(Array.from(finalMap.entries()), options, el, "file:");
}
}
41 changes: 40 additions & 1 deletion src/main.ts
Expand Up @@ -29,6 +29,7 @@ export interface CodeblockOptions {
query: string,
minCount: number,
type: 'unresolved' | 'resolved' | 'both';
shrinkToFit: boolean,
}

export default class TagCloudPlugin extends Plugin {
Expand Down Expand Up @@ -103,7 +104,8 @@ export default class TagCloudPlugin extends Plugin {
rotateRatio: yaml.rotateRatio ? yaml.rotateRatio : 0.1,
query: yaml.query,
minCount: yaml.minCount ? yaml.minCount : 0,
type: yaml.type ? yaml.type : 'resolved'
type: yaml.type ? yaml.type : 'resolved',
shrinkToFit: yaml.shrinkToFit ? yaml.shrinkToFit : true,
}
}

Expand Down Expand Up @@ -186,6 +188,43 @@ export default class TagCloudPlugin extends Plugin {
return words.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
}


generateCloud(values: [string,number][], options: CodeblockOptions, el: HTMLElement, searchPrefix: string) {

const filtered = values.filter(([_, value]) => {
return value >= options.minCount;
});

const canvas = el.createEl('canvas', {attr: {cls: "cloud"}});
canvas.width = options.width;
canvas.height = options.height;

//@ts-ignore
const searchPlugin = this.app.internalPlugins.getPluginById("global-search");
const search = searchPlugin && searchPlugin.instance;

WordCloud(canvas, {
list: filtered,
backgroundColor: options.backgroundColor,
color: options.color,
shape: options.shape,
weightFactor: options.weightFactor,
fontFamily: options.fontFamily,
fontWeight: options.fontWeight,
minSize: options.minFontSize,
minRotation: options.minRotation,
maxRotation: options.maxRotation,
ellipticity: options.ellipticity,
shuffle: options.shuffle,
rotateRatio: options.rotateRatio,
//@ts-ignore
shrinkToFit: options.shrinkToFit,
click: item => {
search.openGlobalSearch(searchPrefix + item[0]);
},
});
}

async onload() {
logger.info("enabling Tag & Word cloud plugin");
await this.loadSettings();
Expand Down
34 changes: 1 addition & 33 deletions src/tagcloud.ts
@@ -1,6 +1,5 @@
import {getAllTags, MarkdownPostProcessorContext} from "obsidian";
import {getAPI} from "obsidian-dataview";
import WordCloud from "wordcloud";
import TagCloudPlugin, {logger} from "./main";

export class TagCloud {
Expand Down Expand Up @@ -65,37 +64,6 @@ export class TagCloud {

el.empty();

const canvas = el.createEl('canvas', {attr: {id: "tagcloud"}});
canvas.width = options.width;
canvas.height = options.height;

//@ts-ignore
const searchPlugin = this.plugin.app.internalPlugins.getPluginById("global-search");
const search = searchPlugin && searchPlugin.instance;

WordCloud(canvas, {
list: filtered,
backgroundColor: options.backgroundColor,
color: options.color,
shape: options.shape,
weightFactor: options.weightFactor,
fontFamily: options.fontFamily,
fontWeight: options.fontWeight,
minSize: options.minFontSize,
minRotation: options.minRotation,
maxRotation: options.maxRotation,
ellipticity: options.ellipticity,
shuffle: options.shuffle,
rotateRatio: options.rotateRatio,
click: item => {
search.openGlobalSearch("tag: " + item[0]);
},
/*
hover: (item, dimension, event) => {
if(item !== undefined) {
console.log(item[0]);
}
}*/
});
this.plugin.generateCloud(filtered, options, el, "tag:");
}
}
33 changes: 2 additions & 31 deletions src/wordcloud.ts
@@ -1,6 +1,5 @@
import TagCloudPlugin, {logger} from "./main";
import TagCloudPlugin from "./main";
import {MarkdownPostProcessorContext, TFile} from "obsidian";
import WordCloud from "wordcloud";

export class Wordcloud {
plugin: TagCloudPlugin;
Expand Down Expand Up @@ -70,44 +69,16 @@ export class Wordcloud {
}
}*/

logger.debug("not filtered ", content);

const filtered = Array.from(content.entries()).filter(([_, v]) => {
return v >= options.minCount;
});
logger.debug("filtered elements", filtered);

el.empty();

if (this.plugin.calculatingWordDistribution) {
el.createEl('p').setText('Word distribution is currently being calculated, reopen this note after calculation has finished');
}

const canvas = el.createEl('canvas', {attr: {id: "wordcloud"}});
canvas.width = options.width;
canvas.height = options.height;

//@ts-ignore
const searchPlugin = this.plugin.app.internalPlugins.getPluginById("global-search");
const search = searchPlugin && searchPlugin.instance;

WordCloud(canvas, {
list: filtered,
backgroundColor: options.backgroundColor,
color: options.color,
shape: options.shape,
weightFactor: options.weightFactor,
fontFamily: options.fontFamily,
fontWeight: options.fontWeight,
minSize: options.minFontSize,
minRotation: options.minRotation,
maxRotation: options.maxRotation,
ellipticity: options.ellipticity,
shuffle: options.shuffle,
rotateRatio: options.rotateRatio,
click: item => {
search.openGlobalSearch(item[0]);
},
});
this.plugin.generateCloud(filtered, options, el, "");
}
}

0 comments on commit 52dd7c4

Please sign in to comment.