Action

收集

Last update almost 6 years ago - Unlisted

来自于action-“Add to list plus”,Posted by dchar

关于标签的修改——在第一个script中(const listTag = ‘资料’;)进行修改

若想删除分类,删除分类的笔记即可

原来动作的下载地址:https://actions.getdrafts.com/a/1Gd

Steps

  • script

    // the tag used for list drafts
    const listTag = '资料';
    
    // set this option to `true` to append
    // or `false` to prepend a new item.
    const appendItem = true;
    
    // set this option to `true` to view
    // the list after adding an item.
    const viewListAfterSuccess = false;
  • script

    // Add to list plus
    
    // Based on an example script from @agiletortoise
    
    (() => {
    
    	const getLists = () => {
    		return ["inbox", "flagged", "archive"]
    			.map(s => Draft.query("", s, [listTag]))
    			.filter(arr => arr !== undefined)
    			.reduce((acc, v) => acc.concat(v), [])
    			.reduce((o, d) => {
    				const m = d.content.match(/## ([^\n]+)/);
    				if (m) { o[m[1].trim()] = d; }
    				return o;
    			}, {});
    	};
    
    	const sortcase = (xs) => {
    		return xs.sort((a,b) => {
    			return a.toLowerCase().localeCompare(b.toLowerCase());
    		})
    	}
    
    	const split = (s, delim) => {
    		const i = s.indexOf(delim);
    		return [
    			s.substr(0, (i>=0) ? i : s.length),
    			(i>=0) ? s.substr(i) : ''
    		]
    	};
    
    	const append = (s, item) => {
    		s = s.replace(/\n$/, '');
    		return [s, item].join('\n');
    	};
    
    	const prepend = (s, item) => {
    		const xs = split(s, '- [');
    		xs[0] = xs[0].replace(/\n$/, '');
    		return [xs[0], item, xs[1]].join('\n');
    	};
    
    	const main = () => {
    
    		if (draft.hasTag(listTag)) {
    			alert('不能运用该动作.');
    			context.fail();
    			return;
    		}
    
    		// prompt to select a list
    		let p = Prompt.create();
    		p.title = '到指定位置';
    
    		const lists = getLists();
    		sortcase(Object.keys(lists)).map(c => p.addButton(c));
    
    		// Give the user an option to create a new list
    		p.addButton('增加你的分类');
    
    		if (!p.show()) {
    			context.fail();
    			return;
    		}
    
    		let d = lists[p.buttonPressed];
    		if (d === undefined) {
    			p = Prompt.create();
    			p.title = '创造你的分类';
    			p.addTextField('分类名称', '请填写', '');
    			p.addButton('Ok');
    
    			if (!p.show()) {
    				context.fail();
    				return;				
    			}
    
    			const name = p.fieldValues['分类名称'].trim();
    			
    			if (name.length == 0) {
    				alert('分类名称无效.');
    				context.fail();
    				return;				
    			}
    
    			d = Draft.create();
    			d.content = `## ${name} \n\n`;
    			d.addTag(listTag);
    		}
    
    		const item = '* ' + draft.content.trim();
    		const fn = appendItem ? append : prepend;
    		d.content = fn(d.content, item);
    		d.update();
    
    		if (viewListAfterSuccess) {
    			editor.load(d);
    		}
    	};
    
    	main();
    
    })();
    

Options

  • After Success Trash
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.