-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15420 from wordpress-mobile/gutenberg/audio-block…
…-processor [Gutenberg] Audio upload processor
- Loading branch information
Showing
4 changed files
with
81 additions
and
6 deletions.
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
54 changes: 54 additions & 0 deletions
54
WordPress/Classes/ViewRelated/Gutenberg/Processors/GutenbergAudioUploadProcessor.swift
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,54 @@ | ||
import Foundation | ||
import Aztec | ||
|
||
class GutenbergAudioUploadProcessor: Processor { | ||
private struct AudioBlockKeys { | ||
static let name = "wp:audio" | ||
static let id = "id" | ||
static let src = "src" | ||
} | ||
|
||
let mediaUploadID: Int32 | ||
let remoteURLString: String | ||
let serverMediaID: Int | ||
|
||
init(mediaUploadID: Int32, serverMediaID: Int, remoteURLString: String) { | ||
self.mediaUploadID = mediaUploadID | ||
self.serverMediaID = serverMediaID | ||
self.remoteURLString = remoteURLString | ||
} | ||
|
||
lazy var fileHtmlProcessor = HTMLProcessor(for: "audio", replacer: { (audio) in | ||
var attributes = audio.attributes | ||
|
||
attributes.set(.string(self.remoteURLString), forKey: AudioBlockKeys.src) | ||
|
||
var html = "<audio " | ||
let attributeSerializer = ShortcodeAttributeSerializer() | ||
html += attributeSerializer.serialize(attributes) | ||
html += "></audio>" | ||
return html | ||
}) | ||
|
||
lazy var fileBlockProcessor = GutenbergBlockProcessor(for: AudioBlockKeys.name, replacer: { fileBlock in | ||
guard let mediaID = fileBlock.attributes[AudioBlockKeys.id] as? Int, | ||
mediaID == self.mediaUploadID else { | ||
return nil | ||
} | ||
var block = "<!-- \(AudioBlockKeys.name) " | ||
var attributes = fileBlock.attributes | ||
attributes[AudioBlockKeys.id] = self.serverMediaID | ||
if let jsonData = try? JSONSerialization.data(withJSONObject: attributes, options: .sortedKeys), | ||
let jsonString = String(data: jsonData, encoding: .utf8) { | ||
block += jsonString | ||
} | ||
block += " -->" | ||
block += self.fileHtmlProcessor.process(fileBlock.content) | ||
block += "<!-- /\(AudioBlockKeys.name) -->" | ||
return block | ||
}) | ||
|
||
func process(_ text: String) -> String { | ||
return fileBlockProcessor.process(text) | ||
} | ||
} |
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