DictationCommand.cpp [plain text]
#include "config.h"
#include "DictationCommand.h"
#include "AlternativeTextController.h"
#include "Document.h"
#include "DocumentMarkerController.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "InsertParagraphSeparatorCommand.h"
#include "InsertTextCommand.h"
#include "Text.h"
namespace WebCore {
class DictationCommandLineOperation {
public:
DictationCommandLineOperation(DictationCommand* dictationCommand)
: m_dictationCommand(dictationCommand)
{ }
void operator()(size_t lineOffset, size_t lineLength, bool isLastLine) const
{
if (lineLength > 0)
m_dictationCommand->insertTextRunWithoutNewlines(lineOffset, lineLength);
if (!isLastLine)
m_dictationCommand->insertParagraphSeparator();
}
private:
DictationCommand* m_dictationCommand;
};
class DictationMarkerSupplier : public TextInsertionMarkerSupplier {
public:
static Ref<DictationMarkerSupplier> create(const Vector<DictationAlternative>& alternatives)
{
return adoptRef(*new DictationMarkerSupplier(alternatives));
}
void addMarkersToTextNode(Text& textNode, unsigned offsetOfInsertion, const String& textToBeInserted) override
{
auto& markerController = textNode.document().markers();
for (auto& alternative : m_alternatives) {
DocumentMarker::DictationData data { alternative.context, textToBeInserted.substring(alternative.range.location, alternative.range.length) };
markerController.addMarker(textNode, alternative.range.location + offsetOfInsertion, alternative.range.length, DocumentMarker::DictationAlternatives, WTFMove(data));
markerController.addMarker(textNode, alternative.range.location + offsetOfInsertion, alternative.range.length, DocumentMarker::SpellCheckingExemption);
}
}
protected:
DictationMarkerSupplier(const Vector<DictationAlternative>& alternatives)
: m_alternatives(alternatives)
{
}
private:
Vector<DictationAlternative> m_alternatives;
};
DictationCommand::DictationCommand(Document& document, const String& text, const Vector<DictationAlternative>& alternatives)
: TextInsertionBaseCommand(document)
, m_textToInsert(text)
, m_alternatives(alternatives)
{
}
void DictationCommand::insertText(Document& document, const String& text, const Vector<DictationAlternative>& alternatives, const VisibleSelection& selectionForInsertion)
{
RefPtr<Frame> frame = document.frame();
ASSERT(frame);
VisibleSelection currentSelection = frame->selection().selection();
String newText = dispatchBeforeTextInsertedEvent(text, selectionForInsertion, false);
RefPtr<DictationCommand> cmd;
if (newText == text)
cmd = DictationCommand::create(document, newText, alternatives);
else
cmd = DictationCommand::create(document, newText, Vector<DictationAlternative>());
applyTextInsertionCommand(frame.get(), *cmd, selectionForInsertion, currentSelection);
}
void DictationCommand::doApply()
{
DictationCommandLineOperation operation(this);
forEachLineInString(m_textToInsert, operation);
postTextStateChangeNotification(AXTextEditTypeDictation, m_textToInsert);
}
void DictationCommand::insertTextRunWithoutNewlines(size_t lineStart, size_t lineLength)
{
Vector<DictationAlternative> alternativesInLine;
collectDictationAlternativesInRange(lineStart, lineLength, alternativesInLine);
auto command = InsertTextCommand::createWithMarkerSupplier(document(), m_textToInsert.substring(lineStart, lineLength), DictationMarkerSupplier::create(alternativesInLine), EditAction::Dictation);
applyCommandToComposite(WTFMove(command), endingSelection());
}
void DictationCommand::insertParagraphSeparator()
{
if (!canAppendNewLineFeedToSelection(endingSelection()))
return;
applyCommandToComposite(InsertParagraphSeparatorCommand::create(document(), false, false, EditAction::Dictation));
}
void DictationCommand::collectDictationAlternativesInRange(size_t rangeStart, size_t rangeLength, Vector<DictationAlternative>& alternatives)
{
for (auto& alternative : m_alternatives) {
if (alternative.range.location >= rangeStart && (alternative.range.location + alternative.range.length) <= rangeStart + rangeLength)
alternatives.append({ { alternative.range.location - rangeStart, alternative.range.length }, alternative.context });
}
}
}