From 13fa1e8a1c600a6a348d119d1299b398cc2f01f1 Mon Sep 17 00:00:00 2001 From: Dio Woo Date: Wed, 17 Jun 2020 18:37:25 +0800 Subject: [PATCH 01/15] fix: notion blocks options --- src/service/export.js | 95 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 14 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index 874b2e0a..95fd137a 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1248,6 +1248,17 @@ class Notion { return "https://www.notion.so/"; } + hasWriteRule(role){ + return role === 'read_and_write' || role === 'editor' + } + + getBlockName(titleArray){ + if (!titleArray) return 'Undefined' + if (Array.isArray(titleArray)) + return titleArray.map((t) => t[0]).join('') + return 'Undefined' + } + UUID() { var __extends=void 0&&(void 0).__extends||function(){var _extendStatics=function extendStatics(d,b){_extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};return _extendStatics(d,b)};return function(d,b){_extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __())}}();var ValueUUID=function(){function ValueUUID(_value){this._value=_value;this._value=_value}ValueUUID.prototype.asHex=function(){return this._value};return ValueUUID}();var V4UUID=function(_super){__extends(V4UUID,_super);function V4UUID(){return _super.call(this,[V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),'-',V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),'-','4',V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),'-',V4UUID._oneOf(V4UUID._timeHighBits),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),'-',V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex(),V4UUID._randomHex()].join(''))||this}V4UUID._oneOf=function(array){return array[Math.floor(array.length*Math.random())]};V4UUID._randomHex=function(){return V4UUID._oneOf(V4UUID._chars)};V4UUID._chars=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];V4UUID._timeHighBits=['8','9','a','b'];return V4UUID}(ValueUUID);function generateUuid(){return new V4UUID().asHex()} return generateUuid(); @@ -1261,20 +1272,76 @@ class Notion { if ( result && status == "success" ) { this.access_token = Object.values( result.recordMap.notion_user )[0].value.id; this.blocks = []; - Object.values( result.recordMap.space ).forEach( item => { - item.value.pages.forEach( ( id, idx ) => { - const block = result.recordMap.block[id]; - idx == 0 && this.blocks.push({ name: item.value.name, value: block.value.id, type: block.value.type }); - if ( block.value.type == "page" ) { - this.blocks.push({ name: "  " + ( block.value.properties ? block.value.properties.title[0][0] : "Undefined" ), value: block.value.id, type: "page" }); - } else if ( block.value.type == "collection_view_page" ) { - Object.values( result.recordMap.collection ).forEach( collection => { - collection.value.parent_id == block.value.id && - this.blocks.push({ name: "  " + collection.value.name[0][0], value: collection.value.id, type: "collection" }); - }); - } - }); - }); + + /** + * 读取所有空间,并创建映射。 + */ + const spaceMaps = {} + Object.values(result.recordMap.space).forEach( + ({ value: spaceValue, role }) => { + if (!this.hasWriteRule(role)) return + spaceMaps[spaceValue.id] = { + name: spaceValue.name, + value: spaceValue.id, + type: 'space', + blocks: [], + } + } + ) + + /** + * 读取所有收藏空间,并创建映射。 + */ + const collectionMaps = {}; + Object.values( + result.recordMap.collection + ).forEach(({ value: collectionValue, role }) => { + if (!this.hasWriteRule(role)) return + collectionMaps[collectionValue.parent_id] = collectionValue + }) + + /** + * 遍历所有当前用户能看到的空间。 + */ + Object.values(result.recordMap.block).forEach( + ({ role, value: blockValue }) => { + if (!this.hasWriteRule(role)) return + const { + type, + space_id, + parent_id, + id, + } = blockValue + + const _space = space_id + ? spaceMaps[space_id] + : spaceMaps[parent_id] + const _spaceBlocks = _space ? _space.blocks : this.blocks + + if (type == 'page') { + _spaceBlocks.push({ + name: '  ' + this.getBlockName(blockValue.properties.title), + value: id, + type: 'page', + }) + } else if (type == 'collection_view_page') { + const collection = collectionMaps[id] + if (!collection) return + _spaceBlocks.push({ + name: '  ' + this.getBlockName(collection.name), + value: collection.id, + type: 'collection', + }) + } + } + ) + + Object.values(spaceMaps).forEach((space) => { + const { blocks, ...spaceAttr } = space + this.blocks.push(spaceAttr) + this.blocks.push(...blocks) + }) + this.type = this.blocks[0].type; this.folder_id = this.blocks[0].value; callback( result, undefined ); From ee17ffc79b0ce0868e5e95f87688c84219b48910 Mon Sep 17 00:00:00 2001 From: Dio Woo Date: Thu, 18 Jun 2020 14:17:06 +0800 Subject: [PATCH 02/15] =?UTF-8?q?*=20=E6=94=B6=E9=9B=86=E5=88=B0Table?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=A2=9E=E5=8A=A0URL=EF=BC=88=E9=99=90?= =?UTF-8?q?=E5=88=B6=EF=BC=9A=E6=8E=88=E6=9D=83=E5=90=8E=E9=80=89=E6=8B=A9?= =?UTF-8?q?block=E6=97=B6=EF=BC=8C=E5=BF=85=E9=A1=BB=E8=A6=81=20type=20=3D?= =?UTF-8?q?=20url=20&&=20name=20=3D=20URL=20=E7=9A=84=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后续尝试优化成自动创建。 --- src/module/authorize.jsx | 8 +++- src/service/export.js | 101 +++++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/src/module/authorize.jsx b/src/module/authorize.jsx index b8b6bdc8..31b60c71 100644 --- a/src/module/authorize.jsx +++ b/src/module/authorize.jsx @@ -287,9 +287,13 @@ export default class Auth extends React.Component { save( state, value ) { state == "pocket" && ( storage.secret.pocket.tags = value.trim() ); state == "linnk" && ( storage.secret.linnk.group_name = value.trim() ); - state == "notion" && ( storage.secret.notion.folder_id = value.trim() ); - state == "notion" && ( storage.secret.notion.type = this.state.notion.filter( item => item.value == value.trim() )[0].type ); state == "youdao" && ( storage.secret.youdao.folder_id = value.trim() ); + if (state == 'notion') { + const notionState = this.state.notion.filter( item => item.value == value.trim() )[0]; + storage.secret.notion.folder_id = value.trim() + storage.secret.notion.type = notionState.type + storage.secret.notion.url_schema_key = notionState.url_schema_key + } storage.Safe( () => this.setState({ secret: storage.secret }), storage.secret ); } diff --git a/src/service/export.js b/src/service/export.js index 95fd137a..e0f4cb45 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1298,11 +1298,34 @@ class Notion { ).forEach(({ value: collectionValue, role }) => { if (!this.hasWriteRule(role)) return collectionMaps[collectionValue.parent_id] = collectionValue + collectionMaps[collectionValue.id] = collectionValue }) /** * 遍历所有当前用户能看到的空间。 */ + const processCollection = (id, spaceBlocks) => { + const collection = collectionMaps[id] + if (!collection) return + + let URLSchemaKey = null; + + Object.keys(collection.schema).some((key) => { + const schema = collection.schema[key] + if (schema.type === 'url' && schema.name === 'URL') { + URLSchemaKey = key; + return true + } + return false + }) + + spaceBlocks.push({ + name: '  ' + this.getBlockName(collection.name), + value: collection.id, + type: 'collection', + url_schema_key: URLSchemaKey, + }) + } Object.values(result.recordMap.block).forEach( ({ role, value: blockValue }) => { if (!this.hasWriteRule(role)) return @@ -1311,6 +1334,7 @@ class Notion { space_id, parent_id, id, + collection_id, } = blockValue const _space = space_id @@ -1319,22 +1343,22 @@ class Notion { const _spaceBlocks = _space ? _space.blocks : this.blocks if (type == 'page') { - _spaceBlocks.push({ - name: '  ' + this.getBlockName(blockValue.properties.title), + _spaceBlocks.push({ + name: + '  ' + + this.getBlockName(blockValue.properties.title), value: id, type: 'page', }) } else if (type == 'collection_view_page') { - const collection = collectionMaps[id] - if (!collection) return - _spaceBlocks.push({ - name: '  ' + this.getBlockName(collection.name), - value: collection.id, - type: 'collection', - }) + processCollection(id, _spaceBlocks) + } else if (type == 'collection_view') { + processCollection(collection_id, _spaceBlocks) } } ) + + Object.values(spaceMaps).forEach((space) => { const { blocks, ...spaceAttr } = space @@ -1524,7 +1548,64 @@ class Notion { }, } } - }), result => callback( result )); + }), result => { + if (this.type == 'collection' && this.url_schema_key) { + result.done && this.CheckQueueTask(result.done.data.taskId, () => { + this.SetProperties(documentId, () => callback(result)) + }) + } else { + callback(result) + } + }); + } + + CheckQueueTask(taskId, callback){ + if (taskId) { + browser.runtime.sendMessage( + msg.Add(msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url: this.url + 'api/v3/getTasks', + data: { + taskIds: [taskId], + }, + }), + (result) => { + if (result.done) { + const { results } = result.done.data + if (results[0].state !== 'success') { + setTimeout(() => { + this.CheckQueueTask(taskId, callback) + }, 500) + } else { + callback() + } + } + } + ) + } + } + + SetProperties(documentId, callback){ + browser.runtime.sendMessage( + msg.Add(msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url: this.url + 'api/v3/submitTransaction', + data: { + operations: [ + { + id: documentId, + table: 'block', + path: ['properties', this.url_schema_key], + command: 'set', + args: [[window.location.href, [['a', window.location.href]]]], + }, + ], + }, + }), + (result) => { + result.done && callback(documentId, undefined) + } + ) } } From 7b46536a50df22a546faf216905006d75d8cea21 Mon Sep 17 00:00:00 2001 From: Dio Woo Date: Thu, 18 Jun 2020 18:10:32 +0800 Subject: [PATCH 03/15] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BAURL=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/export.js | 110 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 5 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index e0f4cb45..e13d0924 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -11,6 +11,7 @@ import * as msg from 'message'; import {browser} from 'browser'; import * as puplugin from 'puplugin'; import * as wiz from 'wiz'; +import {storage} from 'storage' /** * Create PNG @@ -1549,16 +1550,115 @@ class Notion { } } }), result => { - if (this.type == 'collection' && this.url_schema_key) { - result.done && this.CheckQueueTask(result.done.data.taskId, () => { + if (this.type == 'collection') { + /* result.done && this.CheckQueueTask(result.done.data.taskId, () => { this.SetProperties(documentId, () => callback(result)) - }) + }) */ + const taskId = result.done.data.taskId; + if (result.done) { + this.CheckQueueTask(taskId, () => { + if (this.url_schema_key) { + this.SetProperties(documentId, () => callback(result)) + } else { + this.GetCollectionData((collection) => { + this.AddCollectionUrlSchema(collection, (schemaKey) => { + this.url_schema_key = schemaKey + storage.Safe(() => { + storage.secret.notion.url_schema_key = schemaKey + }) + this.SetProperties(documentId, () => callback(result)) + }) + }) + } + }) + } } else { callback(result) } }); } + GetCollectionData(callback){ + browser.runtime.sendMessage( + msg.Add(msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url: this.url + 'api/v3/loadUserContent', + }), + (result) => { + if (result.done) { + const { + recordMap: { collection }, + } = result.done.data + + const currentCollection = collection[this.folder_id].value; + callback(currentCollection) + } + } + ) + } + + AddCollectionUrlSchema(collection, callback){ + const { schema } = collection + const schemaKeys = Object.keys(schema); + let urlSchemaKey = null + schemaKeys.some((key) => { + const schemaItem = schema[key] + if (schemaItem.type === 'url' && schemaItem.name === 'URL') { + urlSchemaKey = key + return true + } + return false + }) + if (urlSchemaKey) { + callback(urlSchemaKey) + return; + } + + const newSchema = { ...schema } + let newSchemaKey = '' + while (!newSchemaKey && schemaKeys.indexOf(newSchemaKey) < 0) { + newSchemaKey = genSchemaKey() + } + + newSchema[newSchemaKey] = { + type: 'url', + name: 'URL', + } + + browser.runtime.sendMessage( + msg.Add(msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url: this.url + 'api/v3/submitTransaction', + data: { + operations: [ + { + args: { + schema: newSchema, + }, + command: 'update', + id: this.folder_id, + path: [], + table: 'collection', + }, + ], + }, + }), + (result) => { + if (result.done) { + callback(newSchemaKey) + } + } + ) + + function genSchemaKey(len = 4){ + let key = ''; + for (; key.length < len; ){ + key += String.fromCharCode(33 + 94 * Math.random()) + } + return key + } + } + CheckQueueTask(taskId, callback){ if (taskId) { browser.runtime.sendMessage( @@ -1575,7 +1675,7 @@ class Notion { if (results[0].state !== 'success') { setTimeout(() => { this.CheckQueueTask(taskId, callback) - }, 500) + }, 1000) } else { callback() } @@ -1597,7 +1697,7 @@ class Notion { table: 'block', path: ['properties', this.url_schema_key], command: 'set', - args: [[window.location.href, [['a', window.location.href]]]], + args: [[window.location.origin, [['a', window.location.href]]]], }, ], }, From fe05c00c16fc98004907fbc8ee3381962a0889b4 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 12:36:00 +0800 Subject: [PATCH 04/15] Format source. --- src/service/export.js | 109 +++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 60 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index e13d0924..03425d88 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1278,18 +1278,16 @@ class Notion { * 读取所有空间,并创建映射。 */ const spaceMaps = {} - Object.values(result.recordMap.space).forEach( - ({ value: spaceValue, role }) => { - if (!this.hasWriteRule(role)) return + Object.values( result.recordMap.space ).forEach(({ value: spaceValue, role }) => { + if (!this.hasWriteRule(role)) return; spaceMaps[spaceValue.id] = { - name: spaceValue.name, - value: spaceValue.id, - type: 'space', - blocks: [], + name : spaceValue.name, + value : spaceValue.id, + type : 'space', + blocks: [], } - } - ) - + }); + /** * 读取所有收藏空间,并创建映射。 */ @@ -1297,75 +1295,66 @@ class Notion { Object.values( result.recordMap.collection ).forEach(({ value: collectionValue, role }) => { - if (!this.hasWriteRule(role)) return - collectionMaps[collectionValue.parent_id] = collectionValue - collectionMaps[collectionValue.id] = collectionValue + if (!this.hasWriteRule(role)) return; + collectionMaps[ collectionValue.parent_id ] = collectionValue; + collectionMaps[ collectionValue.id ] = collectionValue; }) /** * 遍历所有当前用户能看到的空间。 */ - const processCollection = (id, spaceBlocks) => { - const collection = collectionMaps[id] - if (!collection) return - - let URLSchemaKey = null; + const processCollection = ( id, spaceBlocks ) => { + const collection = collectionMaps[id]; + if ( !collection ) return; - Object.keys(collection.schema).some((key) => { - const schema = collection.schema[key] - if (schema.type === 'url' && schema.name === 'URL') { - URLSchemaKey = key; - return true - } - return false - }) + let URLSchemaKey = null; + Object.keys(collection.schema).some( key => { + const schema = collection.schema[key] + if ( schema.type === 'url' && schema.name === 'URL' ) { + URLSchemaKey = key; + return true; + } + return false; + }) - spaceBlocks.push({ - name: '  ' + this.getBlockName(collection.name), - value: collection.id, - type: 'collection', - url_schema_key: URLSchemaKey, - }) + spaceBlocks.push({ + name : '  ' + this.getBlockName( collection.name ), + value: collection.id, + type : 'collection', + url_schema_key: URLSchemaKey, + }) } - Object.values(result.recordMap.block).forEach( - ({ role, value: blockValue }) => { - if (!this.hasWriteRule(role)) return + Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { + if (!this.hasWriteRule(role)) return; const { type, space_id, parent_id, id, collection_id, - } = blockValue + } = blockValue; - const _space = space_id - ? spaceMaps[space_id] - : spaceMaps[parent_id] - const _spaceBlocks = _space ? _space.blocks : this.blocks + const _space = space_id ? spaceMaps[space_id] : spaceMaps[parent_id], + _spaceBlocks = _space ? _space.blocks : this.blocks; if (type == 'page') { - _spaceBlocks.push({ - name: - '  ' + - this.getBlockName(blockValue.properties.title), - value: id, - type: 'page', - }) - } else if (type == 'collection_view_page') { - processCollection(id, _spaceBlocks) - } else if (type == 'collection_view') { - processCollection(collection_id, _spaceBlocks) + _spaceBlocks.push({ + name : '  ' + this.getBlockName( blockValue.properties.title ), + value: id, + type : 'page', + }); + } else if ( type == 'collection_view_page' ) { + processCollection( id, _spaceBlocks ); + } else if ( type == 'collection_view' ) { + processCollection( collection_id, _spaceBlocks ); } - } - ) + }); - - - Object.values(spaceMaps).forEach((space) => { - const { blocks, ...spaceAttr } = space - this.blocks.push(spaceAttr) - this.blocks.push(...blocks) - }) + Object.values( spaceMaps ).forEach( space => { + const { blocks, ...spaceAttr } = space; + this.blocks.push( spaceAttr ); + this.blocks.push( ...blocks ); + }); this.type = this.blocks[0].type; this.folder_id = this.blocks[0].value; From 761b150fb40faaa584a6c1d61524a81188571ca1 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 12:54:55 +0800 Subject: [PATCH 05/15] Optimize notion.blocks data structure. --- src/service/export.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index 03425d88..c57dad0c 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1350,10 +1350,12 @@ class Notion { } }); - Object.values( spaceMaps ).forEach( space => { + Object.values( spaceMaps ).forEach( ( space, idx ) => { const { blocks, ...spaceAttr } = space; - this.blocks.push( spaceAttr ); - this.blocks.push( ...blocks ); + if ( blocks && blocks.length > 0 ) { + this.blocks.push({ name: spaceAttr.name, type: blocks[0].type, value: blocks[0].value, url_schema_key: blocks[0].url_schema_key }); + this.blocks.push( ...blocks ); + } }); this.type = this.blocks[0].type; From 723df2a1308aad0f100848844024a7958560ec49 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 12:57:26 +0800 Subject: [PATCH 06/15] Format source. --- src/service/export.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/export.js b/src/service/export.js index c57dad0c..72f07f93 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1353,7 +1353,7 @@ class Notion { Object.values( spaceMaps ).forEach( ( space, idx ) => { const { blocks, ...spaceAttr } = space; if ( blocks && blocks.length > 0 ) { - this.blocks.push({ name: spaceAttr.name, type: blocks[0].type, value: blocks[0].value, url_schema_key: blocks[0].url_schema_key }); + this.blocks.push({ name: spaceAttr.name, type: blocks[0].type, value: blocks[0].value }); this.blocks.push( ...blocks ); } }); From 10ec636f542d62c1ea0cb0339b6173e20f37d501 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 13:20:56 +0800 Subject: [PATCH 07/15] Format source. --- src/service/export.js | 267 ++++++++++++++++++++---------------------- 1 file changed, 126 insertions(+), 141 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index 72f07f93..fc84df5c 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1350,7 +1350,7 @@ class Notion { } }); - Object.values( spaceMaps ).forEach( ( space, idx ) => { + Object.values( spaceMaps ).forEach( space => { const { blocks, ...spaceAttr } = space; if ( blocks && blocks.length > 0 ) { this.blocks.push({ name: spaceAttr.name, type: blocks[0].type, value: blocks[0].value }); @@ -1541,161 +1541,146 @@ class Notion { } } }), result => { - if (this.type == 'collection') { + if ( this.type == 'collection' ) { /* result.done && this.CheckQueueTask(result.done.data.taskId, () => { this.SetProperties(documentId, () => callback(result)) }) */ const taskId = result.done.data.taskId; - if (result.done) { - this.CheckQueueTask(taskId, () => { - if (this.url_schema_key) { - this.SetProperties(documentId, () => callback(result)) - } else { - this.GetCollectionData((collection) => { - this.AddCollectionUrlSchema(collection, (schemaKey) => { - this.url_schema_key = schemaKey - storage.Safe(() => { - storage.secret.notion.url_schema_key = schemaKey - }) - this.SetProperties(documentId, () => callback(result)) - }) - }) - } - }) - } - } else { - callback(result) - } + if ( result.done ) { + this.CheckQueueTask( taskId, () => { + if ( this.url_schema_key ) { + this.SetProperties( documentId, () => callback( result )); + } else { + this.GetCollectionData( collection => { + this.AddCollectionUrlSchema( collection, schemaKey => { + this.url_schema_key = schemaKey; + storage.Safe(() => { + storage.secret.notion.url_schema_key = schemaKey; + }); + this.SetProperties( documentId, () => callback( result )); + }); + }); + } + }) + } + } else callback( result ); }); } - GetCollectionData(callback){ + GetCollectionData( callback ) { browser.runtime.sendMessage( - msg.Add(msg.MESSAGE_ACTION.AXIOS, { - type: 'post', - url: this.url + 'api/v3/loadUserContent', - }), - (result) => { - if (result.done) { - const { - recordMap: { collection }, - } = result.done.data - - const currentCollection = collection[this.folder_id].value; - callback(currentCollection) - } - } - ) - } - - AddCollectionUrlSchema(collection, callback){ - const { schema } = collection - const schemaKeys = Object.keys(schema); - let urlSchemaKey = null - schemaKeys.some((key) => { - const schemaItem = schema[key] - if (schemaItem.type === 'url' && schemaItem.name === 'URL') { - urlSchemaKey = key - return true - } - return false - }) - if (urlSchemaKey) { - callback(urlSchemaKey) - return; - } - - const newSchema = { ...schema } - let newSchemaKey = '' - while (!newSchemaKey && schemaKeys.indexOf(newSchemaKey) < 0) { - newSchemaKey = genSchemaKey() - } - - newSchema[newSchemaKey] = { - type: 'url', - name: 'URL', - } - - browser.runtime.sendMessage( - msg.Add(msg.MESSAGE_ACTION.AXIOS, { - type: 'post', - url: this.url + 'api/v3/submitTransaction', - data: { - operations: [ - { - args: { - schema: newSchema, - }, - command: 'update', - id: this.folder_id, - path: [], - table: 'collection', - }, - ], - }, - }), - (result) => { - if (result.done) { - callback(newSchemaKey) - } - } - ) - - function genSchemaKey(len = 4){ - let key = ''; - for (; key.length < len; ){ - key += String.fromCharCode(33 + 94 * Math.random()) - } - return key - } - } - - CheckQueueTask(taskId, callback){ - if (taskId) { - browser.runtime.sendMessage( - msg.Add(msg.MESSAGE_ACTION.AXIOS, { - type: 'post', - url: this.url + 'api/v3/getTasks', - data: { - taskIds: [taskId], - }, - }), - (result) => { - if (result.done) { - const { results } = result.done.data - if (results[0].state !== 'success') { - setTimeout(() => { - this.CheckQueueTask(taskId, callback) - }, 1000) - } else { - callback() + msg.Add( msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url : this.url + 'api/v3/loadUserContent', + }), result => { + if (result.done) { + const { + recordMap: { collection }, + } = result.done.data, + currentCollection = collection[ this.folder_id ].value; + callback( currentCollection ); } - } } - ) + ) + } + + AddCollectionUrlSchema( collection, callback ) { + const { schema } = collection, + schemaKeys = Object.keys( schema ), + genSchemaKey = ( len = 4 ) => { + let key = ''; + for ( ; key.length < len; ) { + key += String.fromCharCode( 33 + 94 * Math.random()); + } + return key; + }; + let urlSchemaKey = null; + + schemaKeys.some( key => { + const schemaItem = schema[key]; + if ( schemaItem.type === 'url' && schemaItem.name === 'URL' ) { + urlSchemaKey = key; + return true; + } + return false; + }); + + if ( urlSchemaKey ) { + callback( urlSchemaKey ); + return; + } + + const newSchema = { ...schema }; + let newSchemaKey = ''; + while ( !newSchemaKey && schemaKeys.indexOf(newSchemaKey) < 0 ) { + newSchemaKey = genSchemaKey(); + } + + newSchema[ newSchemaKey ] = { type: 'url', name: 'URL' }; + + browser.runtime.sendMessage( + msg.Add( msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url : this.url + 'api/v3/submitTransaction', + data: { + operations: [{ + args: { + schema: newSchema, + }, + command: 'update', + id: this.folder_id, + path: [], + table: 'collection', + }], + }, + }), result => { + if ( result.done ) { + callback( newSchemaKey ); + } + } + ) + } + + CheckQueueTask( taskId, callback ) { + if ( taskId ) { + browser.runtime.sendMessage( + msg.Add(msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url : this.url + 'api/v3/getTasks', + data: { + taskIds: [taskId], + }, + }), result => { + if ( result.done ) { + const { results } = result.done.data; + if ( results[0].state !== 'success' ) { + setTimeout( () => { + this.CheckQueueTask( taskId, callback ); + }, 1000 ); + } else callback(); + } + } + ) } } - SetProperties(documentId, callback){ + SetProperties( documentId, callback ) { browser.runtime.sendMessage( - msg.Add(msg.MESSAGE_ACTION.AXIOS, { - type: 'post', - url: this.url + 'api/v3/submitTransaction', - data: { - operations: [ - { - id: documentId, - table: 'block', - path: ['properties', this.url_schema_key], - command: 'set', - args: [[window.location.origin, [['a', window.location.href]]]], + msg.Add( msg.MESSAGE_ACTION.AXIOS, { + type: 'post', + url: this.url + 'api/v3/submitTransaction', + data: { + operations: [{ + id: documentId, + table: 'block', + path: ['properties', this.url_schema_key], + command: 'set', + args: [[ window.location.origin, [[ 'a', window.location.href ]]]], + }], }, - ], - }, - }), - (result) => { - result.done && callback(documentId, undefined) - } + }), result => { + result.done && callback( documentId, undefined ); + } ) } From c43a50ee30ea63bf696b30793700f4b3d17142ff Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 13:21:21 +0800 Subject: [PATCH 08/15] Format source. --- src/service/export.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/export.js b/src/service/export.js index fc84df5c..a578f414 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1668,7 +1668,7 @@ class Notion { browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.AXIOS, { type: 'post', - url: this.url + 'api/v3/submitTransaction', + url : this.url + 'api/v3/submitTransaction', data: { operations: [{ id: documentId, From 73ea1f2b81f48761eb13bccb4e9ff455061c981e Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 14:54:23 +0800 Subject: [PATCH 09/15] Change 'url_schema_key' to 'schema' and add notion.schema logic. --- src/module/authorize.jsx | 6 +++--- src/service/export.js | 10 +++++----- src/service/output.js | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/module/authorize.jsx b/src/module/authorize.jsx index 31b60c71..24d6415b 100644 --- a/src/module/authorize.jsx +++ b/src/module/authorize.jsx @@ -290,9 +290,9 @@ export default class Auth extends React.Component { state == "youdao" && ( storage.secret.youdao.folder_id = value.trim() ); if (state == 'notion') { const notionState = this.state.notion.filter( item => item.value == value.trim() )[0]; - storage.secret.notion.folder_id = value.trim() - storage.secret.notion.type = notionState.type - storage.secret.notion.url_schema_key = notionState.url_schema_key + storage.secret.notion.folder_id = value.trim(); + storage.secret.notion.type = notionState.type; + storage.secret.notion.schema = notionState.schema; } storage.Safe( () => this.setState({ secret: storage.secret }), storage.secret ); } diff --git a/src/service/export.js b/src/service/export.js index a578f414..6f7896b1 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1321,7 +1321,7 @@ class Notion { name : '  ' + this.getBlockName( collection.name ), value: collection.id, type : 'collection', - url_schema_key: URLSchemaKey, + schema: URLSchemaKey, }) } Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { @@ -1548,14 +1548,14 @@ class Notion { const taskId = result.done.data.taskId; if ( result.done ) { this.CheckQueueTask( taskId, () => { - if ( this.url_schema_key ) { + if ( this.schema ) { this.SetProperties( documentId, () => callback( result )); } else { this.GetCollectionData( collection => { this.AddCollectionUrlSchema( collection, schemaKey => { - this.url_schema_key = schemaKey; + this.schema = schemaKey; storage.Safe(() => { - storage.secret.notion.url_schema_key = schemaKey; + storage.secret.notion.schema = schemaKey; }); this.SetProperties( documentId, () => callback( result )); }); @@ -1673,7 +1673,7 @@ class Notion { operations: [{ id: documentId, table: 'block', - path: ['properties', this.url_schema_key], + path: ['properties', this.schema], command: 'set', args: [[ window.location.origin, [[ 'a', window.location.href ]]]], }], diff --git a/src/service/output.js b/src/service/output.js index 80d104d2..28e118b0 100644 --- a/src/service/output.js +++ b/src/service/output.js @@ -316,6 +316,7 @@ function action( type, title, desc, content ) { corbLoader( "load", () => { notion.access_token = storage.secret.notion.access_token; notion.folder_id = storage.secret.notion.folder_id; + notion.schema = storage.secret.notion.schema; notion.Add( title, result.replace( /.jpeg!720/ig, '.jpeg' ), ( result, error ) => { exp.svcCbWrapper( result, error, notion.name, type, new Notify() ) }); From 10bf98832b633842a4b0ab527f0681cb32193661 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 14:55:12 +0800 Subject: [PATCH 10/15] Format source. --- src/module/authorize.jsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/module/authorize.jsx b/src/module/authorize.jsx index 24d6415b..5715189e 100644 --- a/src/module/authorize.jsx +++ b/src/module/authorize.jsx @@ -289,10 +289,10 @@ export default class Auth extends React.Component { state == "linnk" && ( storage.secret.linnk.group_name = value.trim() ); state == "youdao" && ( storage.secret.youdao.folder_id = value.trim() ); if (state == 'notion') { - const notionState = this.state.notion.filter( item => item.value == value.trim() )[0]; + const obj = this.state.notion.filter( item => item.value == value.trim() )[0]; storage.secret.notion.folder_id = value.trim(); - storage.secret.notion.type = notionState.type; - storage.secret.notion.schema = notionState.schema; + storage.secret.notion.type = obj.type; + storage.secret.notion.schema = obj.schema; } storage.Safe( () => this.setState({ secret: storage.secret }), storage.secret ); } From 97961700622d0071016ed3608d70778176ceded3 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 15:00:57 +0800 Subject: [PATCH 11/15] Format source. --- src/service/export.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index 6f7896b1..eedf1cb5 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1307,21 +1307,21 @@ class Notion { const collection = collectionMaps[id]; if ( !collection ) return; - let URLSchemaKey = null; - Object.keys(collection.schema).some( key => { - const schema = collection.schema[key] + let schemaKey = null; + Object.keys( collection.schema ).some( key => { + const schema = collection.schema[key]; if ( schema.type === 'url' && schema.name === 'URL' ) { - URLSchemaKey = key; + schemaKey = key; return true; } return false; }) spaceBlocks.push({ - name : '  ' + this.getBlockName( collection.name ), - value: collection.id, - type : 'collection', - schema: URLSchemaKey, + name : '  ' + this.getBlockName( collection.name ), + value : collection.id, + type : 'collection', + schema: schemaKey, }) } Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { @@ -1594,19 +1594,19 @@ class Notion { } return key; }; - let urlSchemaKey = null; + let schemaKey = null; schemaKeys.some( key => { const schemaItem = schema[key]; if ( schemaItem.type === 'url' && schemaItem.name === 'URL' ) { - urlSchemaKey = key; + schemaKey = key; return true; } return false; }); - if ( urlSchemaKey ) { - callback( urlSchemaKey ); + if ( schemaKey ) { + callback( schemaKey ); return; } From 5b7a7537746a566fe7c85c35133b44e04fa092d5 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 15:34:32 +0800 Subject: [PATCH 12/15] Optimize notion.schema workflow. --- src/module/authorize.jsx | 10 +++++----- src/service/export.js | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/module/authorize.jsx b/src/module/authorize.jsx index 5715189e..6d4af2f8 100644 --- a/src/module/authorize.jsx +++ b/src/module/authorize.jsx @@ -288,11 +288,11 @@ export default class Auth extends React.Component { state == "pocket" && ( storage.secret.pocket.tags = value.trim() ); state == "linnk" && ( storage.secret.linnk.group_name = value.trim() ); state == "youdao" && ( storage.secret.youdao.folder_id = value.trim() ); - if (state == 'notion') { - const obj = this.state.notion.filter( item => item.value == value.trim() )[0]; - storage.secret.notion.folder_id = value.trim(); - storage.secret.notion.type = obj.type; - storage.secret.notion.schema = obj.schema; + if ( state == 'notion' ) { + const obj = this.state.notion.filter( item => item.value == value.trim() )[0]; + storage.secret.notion.folder_id = value.trim(); + storage.secret.notion.type = obj.type; + obj.schema && ( storage.secret.notion.schema = obj.schema ); } storage.Safe( () => this.setState({ secret: storage.secret }), storage.secret ); } diff --git a/src/service/export.js b/src/service/export.js index eedf1cb5..95034a0d 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1307,7 +1307,7 @@ class Notion { const collection = collectionMaps[id]; if ( !collection ) return; - let schemaKey = null; + let schemaKey; Object.keys( collection.schema ).some( key => { const schema = collection.schema[key]; if ( schema.type === 'url' && schema.name === 'URL' ) { @@ -1317,12 +1317,13 @@ class Notion { return false; }) - spaceBlocks.push({ + const block = { name : '  ' + this.getBlockName( collection.name ), value : collection.id, type : 'collection', - schema: schemaKey, - }) + }; + schemaKey && ( block.schema = schemaKey ); + spaceBlocks.push( block ); } Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { if (!this.hasWriteRule(role)) return; From 443dbc50dfc00e0be5659e2dce23f07aa5fb2c00 Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 15:34:43 +0800 Subject: [PATCH 13/15] Format source. --- src/service/export.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/service/export.js b/src/service/export.js index 95034a0d..6e5f9293 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1328,11 +1328,11 @@ class Notion { Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { if (!this.hasWriteRule(role)) return; const { - type, - space_id, - parent_id, - id, - collection_id, + type, + space_id, + parent_id, + id, + collection_id, } = blockValue; const _space = space_id ? spaceMaps[space_id] : spaceMaps[parent_id], From 9590e129d38aa1b467750d5ea5d11eeb457119bc Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 15:34:55 +0800 Subject: [PATCH 14/15] Format source. --- src/service/export.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/export.js b/src/service/export.js index 6e5f9293..01894535 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -1326,7 +1326,7 @@ class Notion { spaceBlocks.push( block ); } Object.values( result.recordMap.block ).forEach( ({ role, value: blockValue }) => { - if (!this.hasWriteRule(role)) return; + if ( !this.hasWriteRule( role )) return; const { type, space_id, From f3c3bdfe7225c334d8359fcd70e74be148e25ada Mon Sep 17 00:00:00 2001 From: Kenshin Date: Sat, 20 Jun 2020 15:59:06 +0800 Subject: [PATCH 15/15] Optimize notion.type == collection && notion.schema set and save workflow. --- src/module/authorize.jsx | 1 + src/service/export.js | 10 ++++++---- src/service/output.js | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/module/authorize.jsx b/src/module/authorize.jsx index 6d4af2f8..f317ca72 100644 --- a/src/module/authorize.jsx +++ b/src/module/authorize.jsx @@ -293,6 +293,7 @@ export default class Auth extends React.Component { storage.secret.notion.folder_id = value.trim(); storage.secret.notion.type = obj.type; obj.schema && ( storage.secret.notion.schema = obj.schema ); + obj.type == "page" && delete storage.secret.notion.schema; } storage.Safe( () => this.setState({ secret: storage.secret }), storage.secret ); } diff --git a/src/service/export.js b/src/service/export.js index 01894535..8092d385 100644 --- a/src/service/export.js +++ b/src/service/export.js @@ -11,7 +11,6 @@ import * as msg from 'message'; import {browser} from 'browser'; import * as puplugin from 'puplugin'; import * as wiz from 'wiz'; -import {storage} from 'storage' /** * Create PNG @@ -1555,9 +1554,6 @@ class Notion { this.GetCollectionData( collection => { this.AddCollectionUrlSchema( collection, schemaKey => { this.schema = schemaKey; - storage.Safe(() => { - storage.secret.notion.schema = schemaKey; - }); this.SetProperties( documentId, () => callback( result )); }); }); @@ -1685,6 +1681,12 @@ class Notion { ) } + Save( storage ) { + storage.Safe( () => { + // TO-DO + }, storage.secret ); + } + } /** diff --git a/src/service/output.js b/src/service/output.js index 28e118b0..5f57707c 100644 --- a/src/service/output.js +++ b/src/service/output.js @@ -317,7 +317,13 @@ function action( type, title, desc, content ) { notion.access_token = storage.secret.notion.access_token; notion.folder_id = storage.secret.notion.folder_id; notion.schema = storage.secret.notion.schema; + notion.type = storage.secret.notion.type; notion.Add( title, result.replace( /.jpeg!720/ig, '.jpeg' ), ( result, error ) => { + // hack code + if ( notion.type == "collection" && notion.schema != storage.secret.notion.schema ) { + storage.secret.notion.schema = notion.schema; + notion.Save( storage ); + } exp.svcCbWrapper( result, error, notion.name, type, new Notify() ) }); }, 500 );