-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to drag and drop images and text into MDX (#345)
When dropping an image, the image is downloaded next to the file, and a markdown link is generated. When text is dropped, the text is inserted as-is. This only works in VS Code. This uses a naive text-based approach. This is very similar to the implementation for markdown files that’s builtin to VS Code. Closes #322
- Loading branch information
1 parent
d092cdd
commit e02ea4f
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'vscode-mdx': minor | ||
--- | ||
|
||
Support drag and dropping text and images into the editor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @typedef {import('vscode').DocumentDropEditProvider} DocumentDropEditProvider | ||
* @typedef {import('vscode').DataTransferItem} DataTransferItem | ||
*/ | ||
|
||
import {Uri, WorkspaceEdit} from 'vscode' | ||
import {toMarkdown} from 'mdast-util-to-markdown' | ||
|
||
/** | ||
* @type {DocumentDropEditProvider} | ||
*/ | ||
export const documentDropEditProvider = { | ||
async provideDocumentDropEdits(document, position, dataTransfer) { | ||
/** @type {DataTransferItem | undefined} */ | ||
let textItem | ||
|
||
for (const [mime, item] of dataTransfer) { | ||
if (mime === 'text/plain') { | ||
textItem = item | ||
continue | ||
} | ||
|
||
if (!mime.startsWith('image/')) { | ||
continue | ||
} | ||
|
||
const file = item.asFile() | ||
if (!file) { | ||
continue | ||
} | ||
|
||
const additionalEdit = new WorkspaceEdit() | ||
additionalEdit.createFile(Uri.joinPath(document.uri, '..', file.name), { | ||
contents: file, | ||
ignoreIfExists: true | ||
}) | ||
|
||
return { | ||
insertText: toMarkdown({type: 'image', url: file.name}).trim(), | ||
additionalEdit | ||
} | ||
} | ||
|
||
if (textItem) { | ||
const string = await textItem.asString() | ||
return {insertText: string} | ||
} | ||
|
||
return { | ||
insertText: '' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters