import { IProfile , IDocProfile , profilePreset , docProfilePreset } from "profile" ; import { showHUD } from "utils/common" ; import { Addon , MN } from "const" ; import { updateProfileDataSource } from "./updateDataSource" ; import { MbBookNote } from "types/MarginNote" ; import Base64 from "utils/base64" ; import { layoutViewController } from "jsExtension/switchPanel" ; import lang from "lang" ; import { deepCopy } from "utils" ; let allProfile ; let allDocProfile ; const { profileKey , docProfileKey } = Addon ; const getDataByKey = ( key ) : any => { return NSUserDefaults . standardUserDefaults ( ) . objectForKey ( key ) ; } ; const setDataByKey = ( data , key ) => { NSUserDefaults . standardUserDefaults ( ) . setObjectForKey ( data , key ) ; } ; export const enum Range { First , Doc , Global } const readProfile = ( range , docmd5 = self . docMD5 ?? "init" ) => { switch ( range ) { case Range . First : const docProfileSaved = getDataByKey ( docProfileKey ) ; if ( ! docProfileSaved ) console . log ( "初始化文档配置" , "profile" ) ; allDocProfile = docProfileSaved ?? { [ docmd5 ] : docProfilePreset } ; const profileSaved = getDataByKey ( profileKey ) ; if ( ! profileSaved ) console . log ( "初始化全局配置" , "profile" ) ; allProfile = profileSaved ?? Array ( 5 ) . fill ( profilePreset ) ; case Range . Doc : updateProfileDataSource ( self . docProfile , allDocProfile ?. [ docmd5 ] ?? docProfilePreset ) ; console . log ( "读取当前文档配置" , "profile" ) ; case Range . Global : updateProfileDataSource ( self . profile , allProfile [ self . docProfile . ohmymn . profile [ 0 ] ] , true ) ; console . log ( "读取全局配置" , "profile" ) ; } } ; const saveProfile = ( docmd5 = self . docMD5 , num = self . docProfile . ohmymn . profile [ 0 ] ) => { if ( num != undefined ) { allProfile [ num ] = deepCopy ( self . profile ) ; setDataByKey ( allProfile , profileKey ) ; console . log ( "保存全局配置" , "profile" ) ; } if ( docmd5 != undefined ) { allDocProfile [ docmd5 ] = deepCopy ( self . docProfile ) ; setDataByKey ( allDocProfile , docProfileKey ) ; console . log ( "保存当前文档配置" , "profile" ) ; } } ; const removeProfile = ( ) => { NSUserDefaults . standardUserDefaults ( ) . removeObjectForKey ( profileKey ) ; NSUserDefaults . standardUserDefaults ( ) . removeObjectForKey ( docProfileKey ) ; readProfile ( Range . First ) ; } ; export { saveProfile , readProfile , removeProfile } ; export const manageProfileAction = ( params ) => { const { option , nodes } = params ; const node = nodes [ 0 ] ; if ( option ) { saveProfile ( ) ; node . excerptText = Base64 . encode ( JSON . stringify ( { allProfileTemp : allProfile , allDocProfileTemp : allDocProfile } ) ) ; node . noteTitle = lang . profile_manage . prohibit + new Date ( ) . toLocaleString ( ) ; node . colorIndex = 11 ; } else { const str = node . excerptText ; if ( str ) { try { const { allDocProfileTemp , allProfileTemp } = JSON . parse ( Base64 . decode ( str ) ) ; setDataByKey ( allProfileTemp , Addon . profileKey ) ; setDataByKey ( allDocProfileTemp , Addon . docProfileKey ) ; readProfile ( Range . First ) ; layoutViewController ( ) ; showHUD ( lang . profile_manage . success ) ; } catch { showHUD ( lang . profile_manage . fail ) ; } } else { showHUD ( lang . profile_manage . not_find ) ; } } } ;