On Mobile Joplin, how to get text before selection, after selection, before cursor and after cursor?

Hello everyone! On mobile Joplin, how to get text before selection, after selection, before cursor and after cursor? I can do it as following and this works well on desktop, but can not work on mobile. Is there other ways to do so on mobile app? Thanks!

async function split_note_by_selection() {
	let is_selection_exists = false;
	let content:string = await joplin.commands.execute('editor.execCommand', {
		name: 'getValue',
	}); // can not work on mobile
	if (typeof content ==='string'){
		let lines = content.split('\n'); // split lines
		//
		// selection
		const selectedText = await joplin.commands.execute('selectedText');
		
		if (!selectedText || selectedText.trim() === '') { 
			is_selection_exists = false;
		}
		else{
			is_selection_exists = true;
		}
		//
		// before selection
		let selectionStart = await joplin.commands.execute('editor.execCommand', {
			name: 'getCursor',
			args: ['from'],
		}); // 
		let beforeSelection = lines.slice(0, selectionStart.line).join('\n') + '\n' + lines[selectionStart.line].slice(0, selectionStart.ch);
		//
		// after selection
		let selectionEnd = await joplin.commands.execute('editor.execCommand', {
			name: 'getCursor',
			args: ['to'],
		}); // 
		let afterSelection = lines[selectionEnd.line].slice(selectionEnd.ch) + '\n' + lines.slice(selectionEnd.line + 1).join('\n');
		//
		return {is_selection_exists:is_selection_exists, 
			str_before:beforeSelection, 
			str_selected: selectedText, 
			str_after:afterSelection
		}
	}
}	

Hi,

I think this currently requires creating a Markdown editor plugin that registers a custom command.

1 Like

Thank you. I tried your suggestion, and the plugin works well now. Wonderful.