Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drop images into editor feature #627

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions i18n/fra/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
"asciidoc.preview.openLinksToAsciidocFiles.desc": "Contrôle comment les liens vers des documents AsciiDoc doivent être ouverts.",
"asciidoc.preview.openLinksToAsciidocFiles.inPreview": "Essayer d'ouvrir les liens dans l'éditeur",
"asciidoc.preview.openLinksToAsciidocFiles.inEditor": "Essayer d'ouvrir les liens dans la prévisualisation",
"asciidoc.preview.templates.desc": "Liste de chemins locaux vers des répertoires contenant des modèles personnalisés qui seront utilisés pour la prévisualisation. Les chemins relatifs sont interprétés comme relatifs au répertoire ouvert dans l'Explorer. Si il n'y a pas de répertoire ouvert, ils sont interprétés comme relatifs au fichier AsciiDoc. Les caractères `\\` doivent être échappés `\\\\`.",

"asciidoc.editor.title": "Éditeur",
"asciidoc.editor.enableDrop.desc": "Active le glisser/déposer d'images. Maintenir la touche Shift appuyée afin de déposer un fichier dans l'éditeur de texte.",

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",

"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "Contrôle le moteur PDF à utiliser pour l'export PDF.",
Expand Down Expand Up @@ -70,6 +77,5 @@
"asciidoc.useWorkspaceRoot.desc": "**Obsolète:** Quand on est dans un espace de travail, utilise la racine de l'espace de travail en tant que répertoire de base pour Asciidoctor",
"asciidoc.useWorkspaceRoot.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.useWorkspaceRootAsBaseDirectory#` et n'a plus aucun effet.",
"asciidoc.use_kroki.desc": "**Obsolète:** Active l'extension Kroki afin de générer des diagrammes.",
"asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet.",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/)."
"asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet."
}
10 changes: 8 additions & 2 deletions i18n/jpn/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
"asciidoc.preview.openLinksToAsciidocFiles.desc": "プレビューのAsciiDocファイルのリンクをクリックした場合の動作を選択します。",
"asciidoc.preview.openLinksToAsciidocFiles.inPreview": "リンク先をプレビューで開きます。",
"asciidoc.preview.openLinksToAsciidocFiles.inEditor": "リンク先をエディターで開きます。",
"asciidoc.preview.templates.desc": "List of local paths to custom templates to use from the preview. Relative paths are interpreted relative to the folder open in the Explorer. If there is no open folder, they are interpreted relative to the location of the AsciiDoc file. All `\\` need to be written as `\\\\`.",

"asciidoc.editor.title": "Editor",
"asciidoc.editor.enableDrop.desc": "Enable drag and drop to insert images. Hold down Shift key to drop file into the text editor.",

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.",

"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "PDFエキスポートで使用するPDFエンジンを選択します。",
Expand Down Expand Up @@ -70,6 +77,5 @@
"asciidoc.useWorkspaceRoot.desc": "**非推奨:** ワークスペースのルートパスをベースディレクトリとして使用します。",
"asciidoc.useWorkspaceRoot.deprecationMessage": "本設定は、`#asciidoc.useWorkspaceRootAsBaseDirectory#`に置き換えられ、動作しません。",
"asciidoc.use_kroki.desc": "**非推奨:** ダイアグラム生成Krokiを使用します。",
"asciidoc.use_kroki.deprecationMessage": "本設定は、`#asciidoc.extensions.enableKroki#`に置き換えられ、動作しません。",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support."
"asciidoc.use_kroki.deprecationMessage": "本設定は、`#asciidoc.extensions.enableKroki#`に置き換えられ、動作しません。"
}
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 31 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"browser": "./dist/browser/extension",
"main": "./dist/src/extension.js",
"engines": {
"vscode": "^1.64.0",
"vscode": "^1.70.2",
"node": ">=14"
},
"categories": [
Expand Down Expand Up @@ -267,6 +267,19 @@
"configuration": [
{
"order": 20,
"id": "editor",
"title": "%asciidoc.editor.title%",
"properties": {
"asciidoc.editor.enableDrop": {
"type": "boolean",
"default": true,
"markdownDescription": "%asciidoc.editor.enableDrop.desc%",
"scope": "resource"
}
}
},
{
"order": 21,
"id": "preview",
"title": "%asciidoc.preview.title%",
"properties": {
Expand Down Expand Up @@ -432,7 +445,7 @@
}
},
{
"order": 21,
"order": 22,
"id": "pdf",
"title": "%asciidoc.pdf.title%",
"properties": {
Expand Down Expand Up @@ -487,7 +500,7 @@
}
},
{
"order": 22,
"order": 23,
"id": "extensions",
"title": "%asciidoc.extensions.title%",
"properties": {
Expand All @@ -507,7 +520,7 @@
}
},
{
"order": 23,
"order": 24,
"id": "common",
"title": "%asciidoc.common.title%",
"properties": {
Expand All @@ -521,7 +534,7 @@
}
},
{
"order": 24,
"order": 25,
"id": "debug",
"title": "%asciidoc.debug.title%",
"properties": {
Expand All @@ -543,11 +556,18 @@
"order": 2,
"scope": "resource"
}
},
"asciidoc.antora.enableAntoraSupport": {
"type": "boolean",
"default": false,
"markdownDescription": "%asciidoc.antora.enableAntoraSupport.desc%"
}
},
{
"order": 26,
"id": "antora",
"title": "%asciidoc.antora.title%",
"properties": {
"asciidoc.antora.enableAntoraSupport": {
"type": "boolean",
"default": false,
"markdownDescription": "%asciidoc.antora.enableAntoraSupport.desc%"
}
}
}
],
Expand Down Expand Up @@ -601,7 +621,7 @@
"@types/lodash.throttle": "~4.1",
"@types/mocha": "~9.1",
"@types/node": "~12.20",
"@types/vscode": "~1.64",
"@types/vscode": "~1.70.0",
"@typescript-eslint/eslint-plugin": "5.14.0",
"@typescript-eslint/parser": "5.14.0",
"copy-webpack-plugin": "10.2.4",
Expand Down
8 changes: 6 additions & 2 deletions package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"asciidoc.preview.openLinksToAsciidocFiles.desc": "Contrôle comment les liens vers des documents AsciiDoc doivent être ouverts.",
"asciidoc.preview.openLinksToAsciidocFiles.inPreview": "Essayer d'ouvrir les liens dans l'éditeur",
"asciidoc.preview.openLinksToAsciidocFiles.inEditor": "Essayer d'ouvrir les liens dans la prévisualisation",
"asciidoc.preview.templates.desc": "Liste de chemins locaux vers des répertoires contenant des modèles personnalisés qui seront utilisés pour la prévisualisation. Les chemins relatifs sont interprétés comme relatifs au répertoire ouvert dans l'Explorer. Si il n'y a pas de répertoire ouvert, ils sont interprétés comme relatifs au fichier AsciiDoc. Les caractères `\\` doivent être échappés `\\\\`.",
"asciidoc.editor.title": "Éditeur",
"asciidoc.editor.enableDrop.desc": "Active le glisser/déposer d'images. Maintenir la touche Shift appuyée afin de déposer un fichier dans l'éditeur de texte.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",
"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "Contrôle le moteur PDF à utiliser pour l'export PDF.",
"asciidoc.pdf.engine.asciidoctorPdf.desc": "Ligne de commande asciidoctor-pdf",
Expand Down Expand Up @@ -63,6 +68,5 @@
"asciidoc.useWorkspaceRoot.desc": "**Obsolète:** Quand on est dans un espace de travail, utilise la racine de l'espace de travail en tant que répertoire de base pour Asciidoctor",
"asciidoc.useWorkspaceRoot.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.useWorkspaceRootAsBaseDirectory#` et n'a plus aucun effet.",
"asciidoc.use_kroki.desc": "**Obsolète:** Active l'extension Kroki afin de générer des diagrammes.",
"asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet.",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/)."
"asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet."
}
8 changes: 6 additions & 2 deletions package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"asciidoc.preview.openLinksToAsciidocFiles.desc": "プレビューのAsciiDocファイルのリンクをクリックした場合の動作を選択します。",
"asciidoc.preview.openLinksToAsciidocFiles.inPreview": "リンク先をプレビューで開きます。",
"asciidoc.preview.openLinksToAsciidocFiles.inEditor": "リンク先をエディターで開きます。",
"asciidoc.preview.templates.desc": "List of local paths to custom templates to use from the preview. Relative paths are interpreted relative to the folder open in the Explorer. If there is no open folder, they are interpreted relative to the location of the AsciiDoc file. All `\\` need to be written as `\\\\`.",
"asciidoc.editor.title": "Editor",
"asciidoc.editor.enableDrop.desc": "Enable drag and drop to insert images. Hold down Shift key to drop file into the text editor.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.",
"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "PDFエキスポートで使用するPDFエンジンを選択します。",
"asciidoc.pdf.engine.asciidoctorPdf.desc": "asciidoctor-pdfコマンドライン",
Expand Down Expand Up @@ -63,6 +68,5 @@
"asciidoc.useWorkspaceRoot.desc": "**非推奨:** ワークスペースのルートパスをベースディレクトリとして使用します。",
"asciidoc.useWorkspaceRoot.deprecationMessage": "本設定は、`#asciidoc.useWorkspaceRootAsBaseDirectory#`に置き換えられ、動作しません。",
"asciidoc.use_kroki.desc": "**非推奨:** ダイアグラム生成Krokiを使用します。",
"asciidoc.use_kroki.deprecationMessage": "本設定は、`#asciidoc.extensions.enableKroki#`に置き換えられ、動作しません。",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support."
"asciidoc.use_kroki.deprecationMessage": "本設定は、`#asciidoc.extensions.enableKroki#`に置き換えられ、動作しません。"
}
9 changes: 7 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"asciidoc.preview.openLinksToAsciidocFiles.inEditor": "Try to open links in the preview",
"asciidoc.preview.templates.desc": "List of local paths to custom templates to use from the preview. Relative paths are interpreted relative to the folder open in the Explorer. If there is no open folder, they are interpreted relative to the location of the AsciiDoc file. All `\\` need to be written as `\\\\`.",

"asciidoc.editor.title": "Editor",
"asciidoc.editor.enableDrop.desc": "Enable drag and drop to insert images. Hold down Shift key to drop file into the text editor.",

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.",

"asciidoc.pdf.title": "PDF",
"asciidoc.pdf.engine.desc": "Controls the PDF engine used to export as PDF.",
"asciidoc.pdf.engine.asciidoctorPdf.desc": "asciidoctor-pdf CLI",
Expand Down Expand Up @@ -71,6 +77,5 @@
"asciidoc.useWorkspaceRoot.desc": "**Deprecated:** When in a workspace, uses the workspace root path as the base directory.",
"asciidoc.useWorkspaceRoot.deprecationMessage": "This setting has been replaced by `#asciidoc.useWorkspaceRootAsBaseDirectory#` and no longer has any effect.",
"asciidoc.use_kroki.desc": "**Deprecated:** Enables Kroki integration to generate diagrams.",
"asciidoc.use_kroki.deprecationMessage": "This setting has been replaced by `#asciidoc.extensions.enableKroki#` and no longer has any effect.",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support"
"asciidoc.use_kroki.deprecationMessage": "This setting has been replaced by `#asciidoc.extensions.enableKroki#` and no longer has any effect."
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AttributeReferenceProvider } from './features/attributeReferenceProvide
import { BuiltinDocumentAttributeProvider } from './features/builtinDocumentAttributeProvider'
import AsciidocFoldingRangeProvider from './features/foldingProvider'
import { AntoraSupportManager } from './features/antora/antoraSupport'
import { DropImageIntoEditorProvider } from './features/dropIntoEditor'

export function activate (context: vscode.ExtensionContext) {
const contributionProvider = getAsciidocExtensionContributions(context)
Expand Down Expand Up @@ -58,6 +59,7 @@ export function activate (context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(selector, new AttributeReferenceProvider(), '{'))
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(selector, new BuiltinDocumentAttributeProvider(), ':'))
context.subscriptions.push(vscode.languages.registerFoldingRangeProvider(selector, new AsciidocFoldingRangeProvider()))
context.subscriptions.push(vscode.languages.registerDocumentDropEditProvider(selector, new DropImageIntoEditorProvider()))
const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, previewManager)
const commandManager = new CommandManager()
context.subscriptions.push(commandManager)
Expand Down
80 changes: 80 additions & 0 deletions src/features/dropIntoEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as path from 'path'
import * as vscode from 'vscode'
import * as URI from 'vscode-uri'

const imageFileExtensions = new Set<string>([
'.bmp',
'.gif',
'.ico',
'.jpe',
'.jpeg',
'.jpg',
'.png',
'.svg',
'.tga',
'.tif',
'.tiff',
'.webp',
])

export class DropImageIntoEditorProvider implements vscode.DocumentDropEditProvider {
async provideDocumentDropEdits (document: vscode.TextDocument,
_position: vscode.Position,
dataTransfer: vscode.DataTransfer,
token: vscode.CancellationToken): Promise<vscode.DocumentDropEdit | undefined> {
// Check if drop config is enabled
const enabled = vscode.workspace.getConfiguration('asciidoc', document).get('editor.drop.enabled', true)
if (!enabled) {
return undefined
}

// Return the text or snippet to insert at the drop location.
const snippet = await tryGetUriListSnippet(document, dataTransfer, token)
return snippet ? new vscode.DocumentDropEdit(snippet) : undefined
}
}

async function tryGetUriListSnippet (document: vscode.TextDocument,
dataTransfer: vscode.DataTransfer,
token: vscode.CancellationToken): Promise<vscode.SnippetString | undefined> {
// Get droped files uris
const urlList = await dataTransfer.get('text/uri-list')?.asString()
if (!urlList || token.isCancellationRequested) {
return undefined
}

const uris: vscode.Uri[] = []
for (const resource of urlList.split('\n')) {
uris.push(vscode.Uri.parse(resource.replace('\r', '')))
}

if (!uris.length) {
return
}
// Drop location uri
const docUri = document.uri

const snippet = new vscode.SnippetString()

// Get uri for each uris list value
uris.forEach((uri, i) => {
const imagePath = docUri.scheme === uri.scheme && docUri.authority === uri.authority
? encodeURI(path.relative(URI.Utils.dirname(docUri).fsPath, uri.fsPath).replace(/\\/g, '/'))
: uri.toString(false)

// Check that the droped file is an image
const ext = URI.Utils.extname(uri).toLowerCase()
snippet.appendText(imageFileExtensions.has(ext) ? `image::${imagePath}[]` : '')

// Add a line break if multiple droped documents
if (i <= uris.length - 1 && uris.length > 1) {
snippet.appendText('\n')
}
})
return snippet
}