Skip to content

Commit

Permalink
Start to implement drop into editor feature
Browse files Browse the repository at this point in the history
  • Loading branch information
marieflorescontact committed Aug 25, 2022
1 parent e30084b commit 38444bb
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 22 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

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

14 changes: 10 additions & 4 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 @@ -390,8 +390,8 @@
"markdownDescription": "%asciidoc.preview.templates.desc%",
"items": {
"type": "string"
},
"uniqueItems": false,
},
"uniqueItems": false,
"scope": "resource"
},
"asciidoc.use_kroki": {
Expand Down Expand Up @@ -428,6 +428,12 @@
"markdownDescription": "%asciidoc.use_asciidoctorpdf.desc%",
"markdownDeprecationMessage": "%asciidoc.use_asciidoctorpdf.deprecationMessage%",
"scope": "resource"
},
"asciidoc.editor.drop.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "%configuration.asciidoc.editor.drop.enabled%",
"scope": "resource"
}
}
},
Expand Down Expand Up @@ -601,7 +607,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
5 changes: 3 additions & 2 deletions package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"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.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",
"configuration.asciidoc.editor.drop.enabled": "Active/desactive le glisser-déposer dans l'editeur Asciidoc. Nécessite l'activation `#editor.dropIntoEditor.enabled#`."
}
4 changes: 3 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@
"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.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support",

"configuration.asciidoc.editor.drop.enabled": "Enable/disable dropping into the asciidoc editor to insert shift. Requires enabling `#editor.dropIntoEditor.enabled#`."
}
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
81 changes: 81 additions & 0 deletions src/features/dropIntoEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*---------------------------------------------------------------------------------------------
* 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',
'.psd',
'.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
}

0 comments on commit 38444bb

Please sign in to comment.