complete the new version for add button

This commit is contained in:
Jiaxin Peng
2023-12-29 00:21:21 +00:00
parent cf08703fea
commit 3123d91e97
9 changed files with 130 additions and 26 deletions

View File

@@ -82,7 +82,7 @@ export default class RibbonCommands {
private addCommandForDatabase(dbDetails: DatabaseDetails) {
// Example logic - adjust based on your specific requirements
let commandId = `share-to-${dbDetails.abName}`;
let commandName = `Share to ${dbDetails.abName}`; // or use a translated name
let commandName = `Share to ${dbDetails.fullName} (${dbDetails.abName})`; // or use a translated name
let editorCallback: (editor: Editor, view: MarkdownView) => Promise<void>;
if (dbDetails.format === 'next') {

View File

@@ -81,6 +81,10 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
await this.saveSettings();
}
// previewDatabase(dbDetails: DatabaseDetails) {
//
// }
}

View File

@@ -66,7 +66,7 @@ export class SettingModal extends Modal {
.addOption('none', '')
.addOption('general', i18nConfig.databaseGeneral)
.addOption('next', i18nConfig.databaseNext)
.addOption('custom', i18nConfig.databaseCustom)
// .addOption('custom', i18nConfig.databaseCustom)
.setValue(this.data.databaseFormat)
.onChange(async (value) => {
this.data.databaseFormat = value;
@@ -121,6 +121,31 @@ export class SettingModal extends Modal {
// tag button
this.createSettingEl(nextTabs, i18nConfig.NotionTagButton, i18nConfig.NotionTagButtonDesc, 'toggle', i18nConfig.NotionCustomTitleText, this.data.tagButton, 'tagButton')
// add custom title button
new Setting(nextTabs)
.setName(i18nConfig.NotionCustomTitle)
.setDesc(i18nConfig.NotionCustomTitleDesc)
.addToggle((toggle) =>
toggle
.setValue(this.data.CustomTitleButton)
.onChange(async (value) => {
this.data.CustomTitleButton = value;
this.updateSettingEl(CustomNameEl, value)
// this.updateSettingEl(CustomValuesEl, value)
await this.plugin.saveSettings();
await this.plugin.commands.updateCommand();
})
);
// add custom title name
const CustomNameEl = this.createStyleDiv('custom-name', (this.data.CustomTitleButton), nextTabs);
this.createSettingEl(CustomNameEl, i18nConfig.NotionCustomTitleName, i18nConfig.NotionCustomTitleNameDesc, 'text', i18nConfig.NotionCustomTitleText, this.data.CustomTitleName, 'CustomTitleName')
// add api key
this.createSettingEl(nextTabs, i18nConfig.NotionAPI, i18nConfig.NotionAPIDesc, 'password', i18nConfig.NotionAPIText, this.data.notionAPI, 'notionAPI')

View File

@@ -235,6 +235,18 @@ export class ObsidianSettingTab extends PluginSettingTab {
.setName(`${dbDetails.fullName} (${dbDetails.abName})`)
.setDesc(dbDetails.format)
// add a button for preview data
// settingEl
// .addButton((button: ButtonComponent): ButtonComponent => {
// return button
// .setTooltip("Preview Database")
// .setIcon("eye")
// .onClick(async () => {
// this.plugin.previewDatabase(dbDetails);
// });
// });
// settingEl
// .addButton((button: ButtonComponent): ButtonComponent => {
// return button

View File

@@ -87,13 +87,14 @@ export async function uploadCommandGeneral(
export async function uploadCommandCustom(
plugin: ObsidianSyncNotionPlugin,
settings: PluginSettings,
dbDetails: DatabaseDetails,
app: App,
) {
const { notionAPIGeneral, databaseIDGeneral } = settings;
const { notionAPI, databaseID } = settings;
// Check if the user has set up the Notion API and database ID
if (notionAPIGeneral === "" || databaseIDGeneral === "") {
if (notionAPI === "" || databaseID === "") {
const setAPIMessage = i18nConfig["set-api-id"];
new Notice(setAPIMessage);
return;
@@ -104,7 +105,7 @@ export async function uploadCommandCustom(
if (markDownData) {
const { basename } = nowFile;
const upload = new Upload2NotionCustom(plugin);
const upload = new Upload2NotionCustom(plugin,dbDetails);
const res = await upload.syncMarkdownToNotionCustom(basename, cover, tags, customValues, markDownData, nowFile, this.app);
if (res.status === 200) {

View File

@@ -14,6 +14,7 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
super(plugin);
this.dbDetails = dbDetails;
}
// 因为需要解析notion的block进行对比非常的麻烦
@@ -28,12 +29,13 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
await this.deletePage(notionID);
const { databaseID } = this.dbDetails;
const databasecover = await this.getDataBase(
const databaseCover = await this.getDataBase(
databaseID,
);
if (cover == null) {
cover = databasecover;
cover = databaseCover;
}
return await this.createPage(title, cover, tags, childArr);
@@ -45,13 +47,22 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
tags: string[],
childArr: any,
) {
const {
databaseID,
customTitleButton,
customTitleName,
tagButton,
notionAPI
} = this.dbDetails;
const bodyString: any = {
parent: {
database_id: this.plugin.settings.databaseIDGeneral,
database_id: databaseID,
},
properties: {
[this.plugin.settings.CustomTitleButton
? this.plugin.settings.CustomTitleName
[customTitleButton
? customTitleName
: "title"]: {
title: [
{
@@ -61,7 +72,7 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
},
],
},
...(this.plugin.settings.tagsButton
...(tagButton
? {
tags: {
multi_select: tags && true ? tags.map((tag) => ({ name: tag })) : [],
@@ -98,7 +109,7 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
"Content-Type": "application/json",
// 'User-Agent': 'obsidian.md',
Authorization:
"Bearer " + this.plugin.settings.notionAPIGeneral,
"Bearer " + notionAPI,
"Notion-Version": "2022-06-28",
},
body: JSON.stringify(bodyString),
@@ -144,7 +155,7 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
res = await this.createPage(title, cover, tags, file2Block);
}
if (res.status === 200) {
await updateYamlInfo(markdown, nowFile, res, app, this.plugin);
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails);
} else {
new Notice(`${res.text}`);
}

View File

@@ -40,10 +40,10 @@ export class Upload2NotionNext extends UploadBaseNext {
const { databaseID} = this.dbDetails
const databasecover = await this.getDataBase(databaseID)
const databaseCover = await this.getDataBase(databaseID)
if (cover == null) {
cover = databasecover
cover = databaseCover
}
return await this.createPage(
@@ -77,9 +77,16 @@ export class Upload2NotionNext extends UploadBaseNext {
datetime: string,
childArr: any
) {
const {
databaseID,
notionAPI
} = this.dbDetails
const bodyString: any = {
parent: {
database_id: this.plugin.settings.databaseIDNext
database_id: databaseID,
},
icon: {
emoji: emoji || '📜'
@@ -183,7 +190,7 @@ export class Upload2NotionNext extends UploadBaseNext {
headers: {
'Content-Type': 'application/json',
// 'User-Agent': 'obsidian.md',
'Authorization': 'Bearer ' + this.plugin.settings.notionAPINext,
'Authorization': 'Bearer ' + notionAPI,
'Notion-Version': '2022-06-28',
},
body: JSON.stringify(bodyString),

View File

@@ -4,15 +4,17 @@ import { markdownToBlocks } from "@tryfabric/martian";
import * as yamlFrontMatter from "yaml-front-matter";
// import * as yaml from "yaml"
import MyPlugin from "src/main";
import { PluginSettings } from "../../ui/settingTabs";
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
import { updateYamlInfo } from "../updateYaml";
import {UploadBaseCustom} from "./BaseUpload2NotionCustom";
export class Upload2NotionCustom extends UploadBaseCustom {
settings: PluginSettings;
dbDetails: DatabaseDetails;
constructor(plugin: MyPlugin) {
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
super(plugin);
this.dbDetails = dbDetails;
}
// 因为需要解析notion的block进行对比非常的麻烦
@@ -27,12 +29,14 @@ export class Upload2NotionCustom extends UploadBaseCustom {
) {
await this.deletePage(notionID);
const databasecover = await this.getDataBase(
this.plugin.settings.databaseIDGeneral,
const { databaseID } = this.dbDetails;
const databaseCover = await this.getDataBase(
databaseID
);
if (cover == null) {
cover = databasecover;
cover = databaseCover;
}
return await this.createPage(title, cover, tags, customValues, childArr);
@@ -45,13 +49,22 @@ export class Upload2NotionCustom extends UploadBaseCustom {
customValues: Record<string, string>,
childArr: any,
) {
const {
databaseID,
customTitleButton,
customTitleName,
tagButton,
notionAPI
} = this.dbDetails;
const bodyString: any = {
parent: {
database_id: this.plugin.settings.databaseIDGeneral,
database_id: databaseID,
},
properties: {
[this.plugin.settings.CustomTitleButton
? this.plugin.settings.CustomTitleName
[customTitleButton
? customTitleName
: "title"]: {
title: [
{
@@ -148,7 +161,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
res = await this.createPage(title, cover, tags, customValues, file2Block);
}
if (res.status === 200) {
await updateYamlInfo(markdown, nowFile, res, app, this.plugin);
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails);
} else {
new Notice(`${res.text}`);
}