2022-06-01 12:26:16 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import * as tr from "../lib/ftl";
|
|
|
|
import {
|
|
|
|
ImportExport,
|
|
|
|
importExport,
|
|
|
|
Notetypes,
|
|
|
|
notetypes as notetypeService,
|
|
|
|
} from "../lib/proto";
|
|
|
|
|
|
|
|
export interface ColumnOption {
|
|
|
|
label: string;
|
2022-06-09 02:28:01 +02:00
|
|
|
shortLabel?: string;
|
2022-06-01 12:26:16 +02:00
|
|
|
value: number;
|
|
|
|
disabled: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getColumnOptions(
|
|
|
|
columnLabels: string[],
|
2022-06-09 02:28:01 +02:00
|
|
|
firstRow: string[],
|
2022-06-01 12:26:16 +02:00
|
|
|
notetypeColumn: number | null,
|
|
|
|
deckColumn: number | null,
|
2022-06-09 02:28:01 +02:00
|
|
|
guidColumn: number,
|
2022-06-01 12:26:16 +02:00
|
|
|
): ColumnOption[] {
|
|
|
|
return [{ label: tr.changeNotetypeNothing(), value: 0, disabled: false }].concat(
|
|
|
|
columnLabels.map((label, index) => {
|
|
|
|
index += 1;
|
|
|
|
if (index === notetypeColumn) {
|
|
|
|
return columnOption(tr.notetypesNotetype(), true, index);
|
|
|
|
} else if (index === deckColumn) {
|
|
|
|
return columnOption(tr.decksDeck(), true, index);
|
2022-06-09 02:28:01 +02:00
|
|
|
} else if (index === guidColumn) {
|
|
|
|
return columnOption("GUID", true, index);
|
2022-06-01 12:26:16 +02:00
|
|
|
} else if (label === "") {
|
2022-06-09 02:28:01 +02:00
|
|
|
return columnOption(firstRow[index - 1], false, index, true);
|
2022-06-01 12:26:16 +02:00
|
|
|
} else {
|
2022-06-09 02:28:01 +02:00
|
|
|
return columnOption(label, false, index);
|
2022-06-01 12:26:16 +02:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function columnOption(
|
2022-06-09 02:28:01 +02:00
|
|
|
label: string,
|
2022-06-01 12:26:16 +02:00
|
|
|
disabled: boolean,
|
|
|
|
index: number,
|
2022-06-09 02:28:01 +02:00
|
|
|
shortLabel?: boolean,
|
2022-06-01 12:26:16 +02:00
|
|
|
): ColumnOption {
|
|
|
|
return {
|
2022-06-09 02:28:01 +02:00
|
|
|
label: label ? `${index}: ${label}` : index.toString(),
|
|
|
|
shortLabel: shortLabel ? index.toString() : undefined,
|
2022-06-01 12:26:16 +02:00
|
|
|
value: index,
|
|
|
|
disabled,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getNotetypeFields(notetypeId: number): Promise<string[]> {
|
|
|
|
return notetypeService
|
|
|
|
.getFieldNames(Notetypes.NotetypeId.create({ ntid: notetypeId }))
|
|
|
|
.then((list) => list.vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCsvMetadata(
|
|
|
|
path: string,
|
|
|
|
delimiter?: ImportExport.CsvMetadata.Delimiter,
|
|
|
|
notetypeId?: number,
|
2022-06-09 02:28:01 +02:00
|
|
|
isHtml?: boolean,
|
2022-06-01 12:26:16 +02:00
|
|
|
): Promise<ImportExport.CsvMetadata> {
|
|
|
|
return importExport.getCsvMetadata(
|
|
|
|
ImportExport.CsvMetadataRequest.create({
|
|
|
|
path,
|
|
|
|
delimiter,
|
|
|
|
notetypeId,
|
2022-06-09 02:28:01 +02:00
|
|
|
isHtml,
|
2022-06-01 12:26:16 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|