Skip to content

Commit

Permalink
Merge pull request #41 from PxaMMaxP/refactoring/block-render-components
Browse files Browse the repository at this point in the history
Refactoring/block render components
  • Loading branch information
PxaMMaxP authored Jun 1, 2024
2 parents 6bf90a5 + 416ea18 commit 724df2e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 266 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ data.json
diff-output.txt

list_files.ps1
Projekt.txt
Projekt.txt
diff_output.txt
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "prj",
"name": "Prj Plugin",
"version": "0.4.0",
"version": "0.4.1",
"minAppVersion": "0.15.0",
"description": "Prj Plugin - Project, Document, and Task Management",
"author": "M. Passarello",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-prj-plugin",
"version": "0.4.0",
"version": "0.4.1",
"description": "Prj Plugin - Project, Document, and Task Management",
"main": "main.js",
"scripts": {
Expand Down
100 changes: 5 additions & 95 deletions src/libs/BlockRenderComponents/DocumentBlockRenderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TableBlockRenderComponent, {
BlockRenderSettings,
} from './TableBlockRenderComponent';
import { IProcessorSettings } from '../../interfaces/IProcessorSettings';
import Table, { Row, RowsState, TableHeader } from '../Table';
import Table, { Row, TableHeader } from '../Table';
import Lng from 'src/classes/Lng';
import FilterButton from './InnerComponents/FilterButton';
import MaxShownModelsInput from './InnerComponents/MaxShownModelsInput';
Expand All @@ -14,7 +14,6 @@ import GeneralComponents from './InnerComponents/GeneralComponents';
import API from 'src/classes/API';
import { FileMetadata } from '../MetadataCache';
import Logging from 'src/classes/Logging';
import Search from '../Search/Search';

/**
* Document block render component class for `TableBlockRenderComponent`.
Expand Down Expand Up @@ -213,94 +212,6 @@ export default class DocumentBlockRenderComponent extends TableBlockRenderCompon
return undefined;
}

/**
* This method is called when the search box is used.
* @param search The search text.
* @param key The key that was pressed.
* @returns The search text.
* @remarks - If the `Enter` key was pressed, the search is applied.
* - If the `Escape` key was pressed, the search is reset.
* - After the search is applied, the `onFilter` method is called.
*/
private async onSearch(search: string, key: string): Promise<string> {
if (key === 'Enter') {
if (search !== '') {
this.settings.searchText = search;
this.settings.search = new Search(search);
this.settings.search.parse();
this.onFilter();
} else {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();
}
} else if (key === 'Escape') {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();

return '';
}

return search;
}

/**
* Filters the documents and shows/hides them in the table.
* @remarks - The documents are filtered by the `filter` setting,
* searched by the `search` setting
* and the number of documents is limited by the `maxDocuments` if no search is applied.
*/
private async onFilter() {
this.grayOutHeader();
const batchSize = this.settings.batchSize;
const sleepBetweenBatches = this.settings.sleepBetweenBatches;
let sleepPromise = Promise.resolve();
const documentsLength = this.models.length;
const rows: RowsState[] = [];
let visibleRows = 0;

for (let i = 0; i < documentsLength; i++) {
const document = this.models[i];

const rowUid = this.getUID(document);
let hide = this.getHideState(document, undefined);
this.logger.trace(`Document ${rowUid} is hidden by state: ${hide}`);

this.logger.trace(
`Visible rows: ${visibleRows}; Max shown Docs: ${this.settings.maxDocuments}`,
);

if (visibleRows >= this.settings.maxDocuments) {
hide = true;
}

this.logger.trace(
`Document ${rowUid} is hidden by max counts: ${hide}`,
);

if (hide) {
rows.push({ rowUid, hidden: true });
} else {
visibleRows++;
rows.push({ rowUid, hidden: false });
}

if ((i !== 0 && i % batchSize === 0) || i === documentsLength - 1) {
await sleepPromise;

this.logger.trace(
`Batchsize reached. Change rows: ${rows.length}`,
);
await this.table.changeShowHideStateRows(rows);
rows.length = 0;
sleepPromise = Helper.sleep(sleepBetweenBatches);
}
}

this.normalizeHeader();
}

/**
* Adds the documents to the table.
* @param batchSize The batch size.
Expand Down Expand Up @@ -458,15 +369,15 @@ export default class DocumentBlockRenderComponent extends TableBlockRenderCompon
return row;
}

private getHideState(
document: DocumentModel,
protected getHideState(
model: DocumentModel,
maxVisibleRows: number | undefined,
): boolean {
let searchResult = false;
let maxRows = false;

if (this.settings.search) {
const text = document.data.toString?.();
const text = model.data.toString?.();
searchResult = this.settings.search.applySearchLogic(text ?? '');
}

Expand All @@ -476,8 +387,7 @@ export default class DocumentBlockRenderComponent extends TableBlockRenderCompon
}

const hide =
this.determineHideState(document) ||
this.determineTagHideState(document);
this.determineHideState(model) || this.determineTagHideState(model);

if (searchResult && !hide) {
return false; // Shows the document, if it is not hidden and the search was successful
Expand Down
99 changes: 5 additions & 94 deletions src/libs/BlockRenderComponents/NoteBlockRenderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TableBlockRenderComponent, {
BlockRenderSettings,
} from './TableBlockRenderComponent';
import { IProcessorSettings } from '../../interfaces/IProcessorSettings';
import Table, { Row, RowsState, TableHeader } from '../Table';
import Table, { Row, TableHeader } from '../Table';
import Lng from 'src/classes/Lng';
import MaxShownModelsInput from './InnerComponents/MaxShownModelsInput';
import SearchInput from './InnerComponents/SearchInput';
Expand All @@ -15,7 +15,6 @@ import { NoteModel } from 'src/models/NoteModel';
import ProjectComponents from './InnerComponents/ProjectComponents';
import EditableDataView from '../EditableDataView/EditableDataView';
import Logging from 'src/classes/Logging';
import Search from '../Search/Search';

/**
* Document block render component class for `TableBlockRenderComponent`.
Expand Down Expand Up @@ -148,94 +147,6 @@ export default class NoteBlockRenderComponent extends TableBlockRenderComponent<
return undefined;
}

/**
* This method is called when the search box is used.
* @param search The search text.
* @param key The key that was pressed.
* @returns The search text.
* @remarks - If the `Enter` key was pressed, the search is applied.
* - If the `Escape` key was pressed, the search is reset.
* - After the search is applied, the `onFilter` method is called.
*/
private async onSearch(search: string, key: string): Promise<string> {
if (key === 'Enter') {
if (search !== '') {
this.settings.searchText = search;
this.settings.search = new Search(search);
this.settings.search.parse();
this.onFilter();
} else {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();
}
} else if (key === 'Escape') {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();

return '';
}

return search;
}

/**
* Filters the documents and shows/hides them in the table.
* @remarks - The documents are filtered by the `filter` setting,
* searched by the `search` setting
* and the number of documents is limited by the `maxDocuments` if no search is applied.
*/
private async onFilter() {
this.grayOutHeader();
const batchSize = this.settings.batchSize;
const sleepBetweenBatches = this.settings.sleepBetweenBatches;
let sleepPromise = Promise.resolve();
const documentsLength = this.models.length;
const rows: RowsState[] = [];
let visibleRows = 0;

for (let i = 0; i < documentsLength; i++) {
const document = this.models[i];

const rowUid = this.getUID(document);
let hide = this.determineTagHideState(document);
this.logger.trace(`Document ${rowUid} is hidden by state: ${hide}`);

this.logger.trace(
`Visible rows: ${visibleRows}; Max shown Docs: ${this.settings.maxDocuments}`,
);

if (visibleRows >= this.settings.maxDocuments) {
hide = true;
}

this.logger.trace(
`Document ${rowUid} is hidden by max counts: ${hide}`,
);

if (hide) {
rows.push({ rowUid, hidden: true });
} else {
visibleRows++;
rows.push({ rowUid, hidden: false });
}

if ((i !== 0 && i % batchSize === 0) || i === documentsLength - 1) {
await sleepPromise;

this.logger.trace(
`Batchsize reached. Change rows: ${rows.length}`,
);
await this.table.changeShowHideStateRows(rows);
rows.length = 0;
sleepPromise = Helper.sleep(sleepBetweenBatches);
}
}

this.normalizeHeader();
}

/**
* Adds the documents to the table.
* @param batchSize The batch size.
Expand Down Expand Up @@ -366,15 +277,15 @@ export default class NoteBlockRenderComponent extends TableBlockRenderComponent<
return row;
}

private getHideState(
document: DocumentModel,
protected getHideState(
model: NoteModel,
maxVisibleRows: number | undefined,
): boolean {
let searchResult = false;
let maxRows = false;

if (this.settings.search) {
const text = document.data.toString?.();
const text = model.data.toString?.();
searchResult = this.settings.search.applySearchLogic(text ?? '');
}

Expand All @@ -383,7 +294,7 @@ export default class NoteBlockRenderComponent extends TableBlockRenderComponent<
maxRows = rowStats.visibleRows >= maxVisibleRows;
}

const hide = false;
const hide = this.determineTagHideState(model);

if (searchResult && !hide) {
return false; // Shows the document, if it is not hidden and the search was successful
Expand Down
65 changes: 2 additions & 63 deletions src/libs/BlockRenderComponents/ProjectBlockRenderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TaskData from 'src/types/TaskData';
import TopicData from 'src/types/TopicData';
import ProjectData from 'src/types/ProjectData';
import { IProcessorSettings } from 'src/interfaces/IProcessorSettings';
import Table, { Row, RowsState, TableHeader } from '../Table';
import Table, { Row, TableHeader } from '../Table';
import Lng from 'src/classes/Lng';
import FilterButton from './InnerComponents/FilterButton';
import MaxShownModelsInput from './InnerComponents/MaxShownModelsInput';
Expand All @@ -20,7 +20,6 @@ import { FileMetadata } from '../MetadataCache';
import Logging from 'src/classes/Logging';
import IPrjData from 'src/interfaces/IPrjData';
import IPrjTaskManagement from 'src/interfaces/IPrjTaskManagement';
import Search from '../Search/Search';

export default class ProjectBlockRenderComponent extends TableBlockRenderComponent<
PrjTaskManagementModel<IPrjData & IPrjTaskManagement>
Expand Down Expand Up @@ -362,67 +361,7 @@ export default class ProjectBlockRenderComponent extends TableBlockRenderCompone
return undefined;
}

private async onSearch(search: string, key: string): Promise<string> {
if (key === 'Enter') {
if (search !== '') {
this.settings.searchText = search;
this.settings.search = new Search(search);
this.settings.search.parse();
this.onFilter();
} else {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();
}
} else if (key === 'Escape') {
this.settings.searchText = undefined;
this.settings.search = undefined;
this.onFilter();

return '';
}

return search;
}

private async onFilter() {
this.grayOutHeader();
const batchSize = this.settings.batchSize;
const sleepBetweenBatches = this.settings.sleepBetweenBatches;
let sleepPromise = Promise.resolve();
const documentsLength = this.models.length;
const rows: RowsState[] = [];
let visibleRows = 0;

for (let i = 0; i < documentsLength; i++) {
const document = this.models[i];

const rowUid = this.getUID(document);
let hide = this.getHideState(document, undefined);

if (visibleRows >= this.settings.maxDocuments) {
hide = true;
}

if (hide) {
rows.push({ rowUid, hidden: true });
} else {
visibleRows++;
rows.push({ rowUid, hidden: false });
}

if ((i !== 0 && i % batchSize === 0) || i === documentsLength - 1) {
await sleepPromise;
this.table.changeShowHideStateRows(rows);
rows.length = 0;
sleepPromise = Helper.sleep(sleepBetweenBatches);
}
}

this.normalizeHeader();
}

private getHideState(
protected getHideState(
model: PrjTaskManagementModel<IPrjData & IPrjTaskManagement>,
maxVisibleRows: number | undefined,
): boolean {
Expand Down
Loading

0 comments on commit 724df2e

Please sign in to comment.